<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\SessionAuth;
use App\Repositories\QueueRepository;
use App\Services\QueueService;

final class QueueController extends Controller
{
    public function printForm(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        $this->view('queue/print');
    }

    public function issue(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        $count  = max(1, min(50, (int)($_POST['count'] ?? 1)));
        $prefix = strtoupper(trim((string)($_POST['prefix'] ?? 'A')) ?: 'A');

        $tickets = (new QueueRepository())->issueMany($count, $prefix);
        $this->view('queue/tickets', compact('tickets', 'prefix'));
    }

    public function screen(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        $snap = (new QueueRepository())->snapshot();
        $this->view('queue/screen', compact('snap'));
    }

    /**
     * شاشة تلفزيون بدون تسجيل دخول
     */
    public function tvScreen(): void
    {
        $snap = (new QueueRepository())->snapshot();
        $this->view('queue/tv', compact('snap'));
    }

    public function snapshot(): void
    {
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode(
            (new QueueRepository())->snapshot(),
            JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
        );
    }

    public function callNext(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        (new QueueService())->callNext();
        flash('success', 'تم استدعاء الرقم التالي');
        redirect(base_url('?page=queue_screen'));
    }

    public function done(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        (new QueueService())->completeCurrent();
        flash('success', 'تم إنهاء التذكرة الحالية واحتساب المبلغ حسب الإعدادات');
        redirect(base_url('?page=queue_screen'));
    }

    public function skip(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        (new QueueService())->skipCurrent();
        flash('success', 'تم تخطي التذكرة الحالية');
        redirect(base_url('?page=queue_screen'));
    }

    public function recall(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        $id = (int)($_POST['id'] ?? 0);
        if ($id > 0) {
            (new QueueService())->recallSkipped($id);
            flash('success', 'تم استدعاء الرقم المتخطى، وتمت إعادة الرقم الحالي إلى قائمة الانتظار إن وجد');
        }

        redirect(base_url('?page=queue_screen'));
    }

    public function openDay(): void
    {
        app()->get(SessionAuth::class)->requireLogin();
        verify_csrf();

        (new QueueService())->openNewDay();
        flash('success', 'تم فتح يوم جديد وتصفير تسلسل الطابور');
        redirect(base_url('?page=queue_screen'));
    }

    public function reset(): void
    {
        $this->openDay();
    }
}