<?php

require_once BASE_PATH . '/app/models/Permission.php';

function can(string $permissionKey): bool
{
    $role = $_SESSION['user']['role'] ?? '';
    static $permissionModel = null;

    if ($permissionModel === null) {
        $permissionModel = new Permission();
    }

    return $permissionModel->can($role, $permissionKey);
}

final class Authorization
{
    public static function enforceRoute(string $route): void
    {
        $permissionKey = self::permissionForRoute($route);
        if ($permissionKey === null || !isset($_SESSION['user'])) {
            return;
        }

        if (!can($permissionKey)) {
            http_response_code(403);
            require BASE_PATH . '/app/views/errors/403.php';
            exit;
        }
    }

    public static function permissionForRoute(string $route): ?string
    {
        $exact = [
            'customer' => 'customer.dashboard',
            'customer/booking/create' => 'customer.booking.create',
            'customer/booking/available-slots' => 'customer.booking.create',
            'customer/booking/store' => 'customer.booking.create',
            'customer/bookings' => 'customer.booking.view',
            'customer/booking' => 'customer.booking.view',
            'customer/booking/cancel' => 'customer.booking.view',
            'customer/notifications' => 'customer.notification.view',
            'customer/notification/read' => 'customer.notification.view',
            'customer/notifications/read-all' => 'customer.notification.view',
            'owner' => 'owner.dashboard',
            'owner/bookings' => 'owner.booking.manage',
            'owner/booking' => 'owner.booking.manage',
            'owner/booking/status' => 'owner.booking.manage',
            'owner/booking/note' => 'owner.booking.manage',
            'owner/customers' => 'owner.customer.view',
            'owner/customer' => 'owner.customer.view',
            'owner/schedule' => 'owner.business_hours.manage',
            'owner/business-hours/update' => 'owner.business_hours.manage',
            'owner/blocked-slot/store' => 'owner.business_hours.manage',
            'owner/blocked-slot/delete' => 'owner.business_hours.manage',
            'owner/contacts' => 'owner.contact.manage',
            'owner/contact' => 'owner.contact.manage',
            'owner/contact/status' => 'owner.contact.manage',
            'owner/notifications' => 'owner.notification.view',
            'owner/notification/read' => 'owner.notification.view',
            'owner/notifications/read-all' => 'owner.notification.view',
            'owner/settings' => 'owner.settings.manage',
            'owner/settings/update' => 'owner.settings.manage',
            'owner/social/save' => 'owner.settings.manage',
            'owner/social/toggle' => 'owner.settings.manage',
            'owner/social/delete' => 'owner.settings.manage'
        ];

        if (isset($exact[$route])) {
            return $exact[$route];
        }

        if (str_starts_with($route, 'customer/profile')
            || str_starts_with($route, 'customer/avatar/')
            || $route === 'customer/password/update') {
            return 'customer.profile.manage';
        }
        if (str_starts_with($route, 'owner/service-category')) {
            return 'owner.service_category.manage';
        }
        if (str_starts_with($route, 'owner/service')) {
            return 'owner.service.manage';
        }
        if (str_starts_with($route, 'owner/post-category')) {
            return 'owner.post_category.manage';
        }
        if (str_starts_with($route, 'owner/post')) {
            return 'owner.post.manage';
        }
        if (str_starts_with($route, 'owner/gallery')) {
            return 'owner.gallery.manage';
        }

        return null;
    }
}
