<?php
require_once __DIR__ . '/BaseController.php';

class GuestBlacklistController extends BaseController
{
    public function index(Request $request): void
    {
        $this->view('guest-blacklist/index', [
            'title' => lang('cards.guest_blacklist'),
            'breadcrumbs' => [['label' => lang('cards.guests')], ['label' => lang('cards.guest_blacklist')]],
        ]);
    }

    public function datatable(Request $request): void
    {
        $baseQuery = "SELECT id, guest_name, guest_national_id, guest_phone, reason, blacklisted_date, expires_at, status FROM guest_blacklist WHERE 1=1";
        $columns = [
            ['db' => 'id', 'searchable' => false],
            ['db' => 'guest_name', 'searchable' => true],
            ['db' => 'guest_national_id', 'searchable' => true],
            ['db' => 'guest_phone', 'searchable' => true],
            ['db' => 'reason', 'searchable' => true],
            ['db' => 'blacklisted_date', 'searchable' => false],
            ['db' => 'status', 'searchable' => true],
        ];
        $this->handleDataTable($request, $baseQuery, [], $columns, 'id DESC');
    }

    public function create(Request $request): void
    {
        $this->view('guest-blacklist/create', [
            'title' => lang('cards.add_to_blacklist'),
            'breadcrumbs' => [['label' => lang('cards.guests')], ['label' => lang('cards.guest_blacklist'), 'url' => '/guest-blacklist'], ['label' => lang('common.create')]],
        ]);
    }

    public function store(Request $request): void
    {
        if (!CSRF::validate($request->csrfToken())) { $this->json(['success' => false, 'message' => 'Invalid token'], 403); return; }
        $validator = new Validator($request->all(), [
            'guest_name' => 'required|string|max:300',
            'reason' => 'required|string',
        ]);
        if ($validator->fails()) { $this->json(['success' => false, 'errors' => $validator->errors()], 422); return; }

        $db = Database::getInstance();
        $id = $db->insert('guest_blacklist', [
            'guest_name' => $request->post('guest_name'),
            'guest_national_id' => $request->post('guest_national_id') ?: null,
            'guest_phone' => $request->post('guest_phone') ?: null,
            'reason' => $request->post('reason'),
            'blacklisted_date' => date('Y-m-d'),
            'blacklisted_by' => Auth::id(),
            'expires_at' => $request->post('expires_at') ?: null,
            'status' => 'active',
            'created_at' => date('Y-m-d H:i:s'),
        ]);
        AuditLog::log('create', 'cards', 'guest_blacklist', $id, "Guest blacklisted: " . $request->post('guest_name'));
        $this->json(['success' => true, 'message' => lang('cards.blacklist_added'), 'redirect' => '/guest-blacklist']);
    }

    public function remove(Request $request): void
    {
        if (!CSRF::validate($request->csrfToken())) { $this->json(['success' => false, 'message' => 'Invalid token'], 403); return; }
        $id = (int) $request->routeParam('id');
        $db = Database::getInstance();
        $db->update('guest_blacklist', [
            'status' => 'removed',
            'removed_by' => Auth::id(),
            'removed_at' => date('Y-m-d H:i:s'),
            'removal_reason' => $request->post('reason', ''),
        ], 'id = ?', [$id]);
        AuditLog::log('update', 'cards', 'guest_blacklist', $id, "Removed from blacklist");
        $this->json(['success' => true, 'message' => lang('cards.blacklist_removed')]);
    }
}