<?php
declare(strict_types=1);

namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/conference_scope.php';

use App\Core\Controller;
use App\Core\Database;

final class AttendanceController extends Controller
{
    public function checkin(): void
    {
        require_login();
        $db=Database::getInstance();

        $session_id=(int)($_GET['session_id']??0);
        if($session_id<=0){ http_response_code(404); echo "Missing session_id"; return; }

        // derive conference_id from session
        $st=$db->prepare("SELECT conference_id FROM conference_sessions WHERE id=:id LIMIT 1");
        $st->execute([':id'=>$session_id]);
        $cid=(int)($st->fetchColumn()?:0);
        if($cid<=0){ http_response_code(404); echo "Session not found"; return; }

        $uid=(int)auth_id();
        $db->prepare("INSERT IGNORE INTO attendance_logs(conference_id,session_id,user_id,method) VALUES(:c,:s,:u,'web')")
           ->execute([':c'=>$cid,':s'=>$session_id,':u'=>$uid]);

        if (session_status() !== PHP_SESSION_ACTIVE) session_start();
        $_SESSION['flash_success']='Checked-in successfully.';
        header('Location: '.base_path('author')); exit;
    }
}
