<?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 AdminQuickActionsController extends Controller
{
    public function index(): void
    {
        require_permission('admin.access');
        $db = Database::getInstance();

        $cid = \function_exists('current_conference_id') ? (int)\current_conference_id() : 0;
        $confs = [];
        try { $confs = $db->query("SELECT id,title_en,title_ar FROM conferences WHERE (is_active=1 OR is_active IS NULL) ORDER BY id DESC")->fetchAll(); } catch (\Throwable $e) {}

        $this->view('admin/quick_actions/index', [
            'conference_id'=>$cid,
            'conferences'=>$confs
        ]);
    }

    public function autoAssign(): void
    {
        require_permission('admin.access');
        if (!$this->isPost()) redirect('admin/quick-actions');
        csrf_check();

        // Reuse existing auto assign endpoint if exists
        $cid = (int)($_POST['conference_id'] ?? 0);
        $rn  = (int)($_POST['round_no'] ?? 1);
        $pp  = max(1, (int)($_POST['per_paper'] ?? 2));

        // Redirect to your existing controller if present
        // If admin/review-assignments/auto exists, forward there
        $_POST['conference_id'] = $cid;
        $_POST['round_no'] = $rn;
        $_POST['per_paper'] = $pp;

        redirect('admin/review-assignments?conference_id='.$cid.'&round_no='.$rn);
    }

    public function cloneRound(): void
    {
        require_permission('admin.access');
        if (!$this->isPost()) redirect('admin/quick-actions');
        csrf_check();

        $cid = (int)($_POST['conference_id'] ?? 0);
        $from = (int)($_POST['from_round'] ?? 1);
        $to = (int)($_POST['to_round'] ?? 2);

        // If existing clone route exists, just redirect there:
        redirect('admin/review-assignments?conference_id='.$cid.'&round_no='.$to);
    }
}
