<?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 AdminScheduleController extends Controller
{
    public function index(): void
    {
        require_admin_area();
        $db = Database::getInstance();

        $conf = (int)($_GET['conference_id'] ?? 0);
        if ($conf <= 0) $conf = (int)(function_exists('default_conference_id') ? default_conference_id() : 0);

        // days
        $days = [];
        $st = $db->prepare("SELECT * FROM conference_days WHERE conference_id=:c ORDER BY sort_order ASC, day_date ASC, id ASC");
        $st->execute([':c'=>$conf]);
        $days = $st->fetchAll() ?: [];

        // sessions
        $sessions = [];
        $st = $db->prepare("SELECT * FROM conference_sessions WHERE conference_id=:c ORDER BY COALESCE(session_date,'2099-12-31') ASC, start_time ASC, id ASC");
        $st->execute([':c'=>$conf]);
        $sessions = $st->fetchAll() ?: [];

        $this->view('admin/schedule/index', compact('conf','days','sessions'));
    }

    public function daySave(): void
    {
        require_admin_area();
        $db = Database::getInstance();

        $id = (int)($_POST['id'] ?? 0);
        $conf = (int)($_POST['conference_id'] ?? 0);
        $date = trim((string)($_POST['day_date'] ?? ''));
        $en = trim((string)($_POST['title_en'] ?? ''));
        $ar = trim((string)($_POST['title_ar'] ?? ''));
        $sort = (int)($_POST['sort_order'] ?? 0);

        if ($conf<=0 || $date==='') { if(function_exists('flash_set')) flash_set('danger','Missing conference/date'); header('Location: '.base_path('admin/schedule')); exit; }

        if ($id>0) {
            $db->prepare("UPDATE conference_days SET day_date=:d,title_en=:e,title_ar=:a,sort_order=:s WHERE id=:id")->execute([
              ':d'=>$date,':e'=>($en?:null),':a'=>($ar?:null),':s'=>$sort,':id'=>$id
            ]);
            if(function_exists('flash_set')) flash_set('success','Day updated');
        } else {
            $db->prepare("INSERT INTO conference_days(conference_id,day_date,title_en,title_ar,sort_order) VALUES(:c,:d,:e,:a,:s)")->execute([
              ':c'=>$conf,':d'=>$date,':e'=>($en?:null),':a'=>($ar?:null),':s'=>$sort
            ]);
            if(function_exists('flash_set')) flash_set('success','Day added');
        }
        header('Location: '.base_path('admin/schedule?conference_id='.$conf));
        exit;
    }

    public function sessionSave(): void
    {
        require_admin_area();
        $db = Database::getInstance();

        $id = (int)($_POST['id'] ?? 0);
        $conf = (int)($_POST['conference_id'] ?? 0);
        $dayId = (int)($_POST['day_id'] ?? 0);
        $date = trim((string)($_POST['session_date'] ?? ''));
        $stt = trim((string)($_POST['start_time'] ?? ''));
        $edt = trim((string)($_POST['end_time'] ?? ''));
        $en = trim((string)($_POST['title_en'] ?? ''));
        $ar = trim((string)($_POST['title_ar'] ?? ''));
        $loc = trim((string)($_POST['location'] ?? ''));
        $chair = trim((string)($_POST['chair_name'] ?? ''));

        if ($conf<=0) { if(function_exists('flash_set')) flash_set('danger','Missing conference'); header('Location: '.base_path('admin/schedule')); exit; }

        if ($id>0) {
            $db->prepare("UPDATE conference_sessions SET day_id=:day,session_date=:d,start_time=:st,end_time=:et,title_en=:e,title_ar=:a,location=:l,chair_name=:ch WHERE id=:id")->execute([
              ':day'=>($dayId?:null),':d'=>($date?:null),':st'=>($stt?:null),':et'=>($edt?:null),
              ':e'=>($en?:null),':a'=>($ar?:null),':l'=>($loc?:null),':ch'=>($chair?:null),':id'=>$id
            ]);
            if(function_exists('flash_set')) flash_set('success','Session updated');
        } else {
            $db->prepare("INSERT INTO conference_sessions(conference_id,day_id,session_date,start_time,end_time,title_en,title_ar,location,chair_name) VALUES(:c,:day,:d,:st,:et,:e,:a,:l,:ch)")->execute([
              ':c'=>$conf,':day'=>($dayId?:null),':d'=>($date?:null),':st'=>($stt?:null),':et'=>($edt?:null),
              ':e'=>($en?:null),':a'=>($ar?:null),':l'=>($loc?:null),':ch'=>($chair?:null)
            ]);
            if(function_exists('flash_set')) flash_set('success','Session added');
        }

        header('Location: '.base_path('admin/schedule?conference_id='.$conf));
        exit;
    }

    public function delete(): void
    {
        require_admin_area();
        $db = Database::getInstance();
        $type = (string)($_GET['type'] ?? '');
        $id = (int)($_GET['id'] ?? 0);
        $conf = (int)($_GET['conference_id'] ?? 0);

        if ($id>0 && $type==='day') $db->prepare("DELETE FROM conference_days WHERE id=:id")->execute([':id'=>$id]);
        if ($id>0 && $type==='session') $db->prepare("DELETE FROM conference_sessions WHERE id=:id")->execute([':id'=>$id]);

        if(function_exists('flash_set')) flash_set('success','Deleted');
        header('Location: '.base_path('admin/schedule?conference_id='.$conf));
        exit;
    }
}
