<?php
declare(strict_types=1);

namespace App\Controllers;

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

final class AdminChairsTenantController extends Controller
{
    public function index(): void
    {
        require_admin_area();
        require_org_context();

        $db = Database::getInstance();
        $orgId = (int) current_org_id();

        // conferences in org
        $st = $db->prepare("SELECT id, title_en, title_ar FROM conferences WHERE org_id=:o ORDER BY id DESC");
        $st->execute([':o'=>$orgId]);
        $confs = $st->fetchAll() ?: [];

        $confId = (int)($_GET['conference_id'] ?? ($confs[0]['id'] ?? 0));
        if ($confId > 0) require_conference_of_org($confId);

        $rows = [];
        if ($confId > 0) {
            $st = $db->prepare("
              SELECT cc.*, c.title_en AS conf_title_en, c.title_ar AS conf_title_ar
              FROM conference_chairs cc
              JOIN conferences c ON c.id=cc.conference_id
              WHERE c.org_id=:o AND cc.conference_id=:c
              ORDER BY cc.id DESC
            ");
            $st->execute([':o'=>$orgId, ':c'=>$confId]);
            $rows = $st->fetchAll() ?: [];
        }

        $this->view('admin/chairs/index', compact('rows','confs','confId'));
    }

    public function save(): void
    {
        require_admin_area();
        require_org_context();
        if(($_SERVER['REQUEST_METHOD']??'GET')!=='POST'){ http_response_code(405); return; }

        $db=Database::getInstance();
        $orgId=(int)current_org_id();

        $id=(int)($_POST['id']??0);
        $confId=(int)($_POST['conference_id']??0);
        require_conference_of_org($confId);

        $full=trim((string)($_POST['full_name']??''));
        $email=trim((string)($_POST['email']??''));
        $role=trim((string)($_POST['chair_role']??'tpc'));
        $aff=trim((string)($_POST['affiliation']??''));
        $active=(int)($_POST['is_active']??1);

        if($full===''){
          if(function_exists('flash_set')) flash_set('danger','Missing name');
          header('Location: '.base_path('admin/chairs?conference_id='.$confId)); exit;
        }

        if($id>0){
          $st=$db->prepare("
            UPDATE conference_chairs cc
            JOIN conferences c ON c.id=cc.conference_id
            SET cc.full_name=:n, cc.email=:e, cc.chair_role=:r, cc.affiliation=:a, cc.is_active=:ia
            WHERE cc.id=:id AND c.org_id=:o
          ");
          $st->execute([':n'=>$full,':e'=>($email!==''?$email:null),':r'=>$role,':a'=>($aff!==''?$aff:null),':ia'=>$active,':id'=>$id,':o'=>$orgId]);
          if(function_exists('flash_set')) flash_set('success','Saved');
        } else {
          $st=$db->prepare("
            INSERT INTO conference_chairs(conference_id,full_name,email,chair_role,affiliation,is_active)
            VALUES(:c,:n,:e,:r,:a,:ia)
          ");
          $st->execute([':c'=>$confId,':n'=>$full,':e'=>($email!==''?$email:null),':r'=>$role,':a'=>($aff!==''?$aff:null),':ia'=>$active]);
          if(function_exists('flash_set')) flash_set('success','Created');
        }

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

    public function delete(): void
    {
        require_admin_area();
        require_org_context();
        $db=Database::getInstance();
        $orgId=(int)current_org_id();

        $id=(int)($_GET['id']??0);
        $confId=(int)($_GET['conference_id']??0);
        if($confId>0) require_conference_of_org($confId);

        if($id>0){
          $st=$db->prepare("
            DELETE cc FROM conference_chairs cc
            JOIN conferences c ON c.id=cc.conference_id
            WHERE cc.id=:id AND c.org_id=:o
          ");
          $st->execute([':id'=>$id,':o'=>$orgId]);
        }
        header('Location: '.base_path('admin/chairs?conference_id='.$confId)); exit;
    }
}
