<?php
declare(strict_types=1);

ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

require_once dirname(__DIR__) . '/app/bootstrap.php';

use App\Controllers\AuthController;
use App\Controllers\DashboardController;
use App\Controllers\InvoiceController;
use App\Controllers\ItemController;
use App\Controllers\PublicController;
use App\Controllers\QueueController;
use App\Controllers\ReturnController;
use App\Controllers\ReportController;
use App\Controllers\SettingsController;
use App\Controllers\AuditController;
use App\Controllers\UsersController;
use App\Controllers\TailoringController;
use App\Controllers\CustomersController;
use App\Controllers\CategoriesController;
use App\Controllers\LocationsController;
use App\Controllers\StudioController;
use App\Core\Router;

try {
    $auth = app()->get(App\Core\SessionAuth::class);
    $page = $_GET['page'] ?? ($auth->check() ? 'dashboard' : 'login');

    $router = new Router();

    $router->get('login', [AuthController::class, 'login']);
    $router->post('login', [AuthController::class, 'authenticate']);
    $router->get('logout', [AuthController::class, 'logout']);

    $router->get('dashboard', [DashboardController::class, 'index']);

    $router->get('items', [ItemController::class, 'index']);
    $router->get('available_items', [ItemController::class, 'available']);
    $router->get('item_public', [ItemController::class, 'publicSchedule']);
    $router->get('item_barcode', [ItemController::class, 'printBarcode']);
    $router->get('item_barcodes_bulk', [ItemController::class, 'printBulkBarcodes']);
    $router->get('item_create', [ItemController::class, 'create']);
    $router->post('item_store', [ItemController::class, 'store']);
    $router->get('item_edit', [ItemController::class, 'edit']);
    $router->post('item_update', [ItemController::class, 'update']);
    $router->post('item_delete', [ItemController::class, 'delete']);

    $router->get('categories', [CategoriesController::class, 'index']);
    $router->post('category_store', [CategoriesController::class, 'store']);
    $router->post('category_update', [CategoriesController::class, 'update']);
    $router->post('category_move_items', [CategoriesController::class, 'moveItems']);
    $router->post('category_delete', [CategoriesController::class, 'delete']);

    $router->get('locations', [LocationsController::class, 'index']);
    $router->post('location_store', [LocationsController::class, 'store']);
    $router->post('location_update', [LocationsController::class, 'update']);
    $router->post('location_delete', [LocationsController::class, 'delete']);

    $router->get('invoices', [InvoiceController::class, 'index']);
    $router->post('invoices', [InvoiceController::class, 'store']);
    $router->get('invoice_show', [InvoiceController::class, 'show']);
    $router->get('customer_lookup', [InvoiceController::class, 'customerLookup']);
    $router->get('customer_search', [InvoiceController::class, 'customerSearch']);

    $router->get('queue_print', [QueueController::class, 'printForm']);
    $router->post('queue_print', [QueueController::class, 'issue']);

    $router->get('queue_public', [PublicController::class, 'queuePublic']);
    $router->post('queue_public', [PublicController::class, 'queuePublic']);

    $router->get('queue_screen', [QueueController::class, 'screen']);
    $router->get('queue_snapshot', [QueueController::class, 'snapshot']);

    $router->post('queue_call_next', [QueueController::class, 'callNext']);
    $router->post('queue_done', [QueueController::class, 'done']);
    $router->post('queue_skip', [QueueController::class, 'skip']);
    $router->post('queue_recall', [QueueController::class, 'recall']);
    $router->post('queue_open_day', [QueueController::class, 'openDay']);
    $router->post('queue_reset', [QueueController::class, 'reset']);

    // شاشة تلفزيون بدون تسجيل دخول
    $router->get('queue_tv', [QueueController::class, 'tvScreen']);

    $router->get('returns', [ReturnController::class, 'index']);
    $router->get('return_show', [ReturnController::class, 'show']);
    $router->post('return_process', [ReturnController::class, 'process']);

    $router->get('reports', [ReportController::class, 'index']);
    $router->get('queue_reports', [ReportController::class, 'index']);

    $router->get('settings_branding', [SettingsController::class, 'branding']);
    $router->post('settings_branding_save', [SettingsController::class, 'saveBranding']);
    $router->get('settings_whatsapp', [SettingsController::class, 'whatsapp']);
    $router->post('settings_whatsapp_save', [SettingsController::class, 'saveWhatsapp']);
    $router->get('queue_settings', [SettingsController::class, 'whatsapp']);

    $router->get('audit', [AuditController::class, 'index']);


    $router->get('tailoring', [TailoringController::class, 'index']);
    $router->post('tailoring_store', [TailoringController::class, 'store']);
    $router->get('tailoring_print', [TailoringController::class, 'print']);
    $router->post('tailoring_ready', [TailoringController::class, 'markReady']);
    $router->get('customers', [CustomersController::class, 'index']);
    $router->post('customer_blacklist', [CustomersController::class, 'blacklist']);
    $router->post('customer_unblacklist', [CustomersController::class, 'unblacklist']);

    $router->get('studio', [StudioController::class, 'index']);
    $router->post('studio_store', [StudioController::class, 'store']);
    $router->post('studio_update', [StudioController::class, 'update']);
    $router->post('studio_delete', [StudioController::class, 'delete']);
    $router->get('studio_booked_slots', [StudioController::class, 'bookedSlots']);

    $router->get('users', [UsersController::class, 'index']);
    $router->get('user_create', [UsersController::class, 'create']);
    $router->post('user_store', [UsersController::class, 'store']);
    $router->get('user_edit', [UsersController::class, 'edit']);
    $router->post('user_update', [UsersController::class, 'update']);
    $router->post('user_delete', [UsersController::class, 'delete']);

    $publicPages = ['login', 'queue_public', 'item_public', 'queue_tv', 'queue_snapshot'];
    if (!in_array((string)$page, $publicPages, true)) {
        $auth->requirePageAccess((string)$page);
    }

    $router->dispatch((string)$page, $_SERVER['REQUEST_METHOD'] ?? 'GET');
} catch (Throwable $e) {
    http_response_code(500);
    echo '<h2 style="font-family:Arial,sans-serif;color:#b91c1c">Fatal Error</h2>';
    echo '<pre style="background:#f8fafc;border:1px solid #cbd5e1;padding:16px;white-space:pre-wrap;">';
    echo htmlspecialchars($e->getMessage()) . "\n\n";
    echo htmlspecialchars($e->getFile()) . ':' . (int)$e->getLine() . "\n\n";
    echo htmlspecialchars($e->getTraceAsString());
    echo '</pre>';
}