<?php
/**
 * Booking staff access: host role and capabilities.
 *
 * @package Maca_MenuList
 */

if (!defined('ABSPATH')) {
    exit;
}

if (!defined('MACA_MENULIST_BOOKING_HOST_ROLE')) {
    define('MACA_MENULIST_BOOKING_HOST_ROLE', 'maca_booking_host');
}

if (!defined('MACA_MENULIST_CAP_MANAGE_BOOKINGS')) {
    define('MACA_MENULIST_CAP_MANAGE_BOOKINGS', 'maca_manage_bookings');
}

if (!defined('MACA_MENULIST_CAP_EDIT_BOOKINGS')) {
    define('MACA_MENULIST_CAP_EDIT_BOOKINGS', 'maca_edit_bookings');
}

if (!defined('MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE')) {
    define('MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE', 'maca_manage_booking_schedule');
}

/**
 * Remove legacy PIN / public staff page options.
 *
 * @return void
 */
function maca_menulist_remove_booking_staff_pin_options() {
    if (get_option('maca_menulist_booking_staff_pin_removed') === MACA_MENULIST_VERSION) {
        return;
    }

    delete_option('maca_menulist_booking_staff_mode');
    delete_option('maca_menulist_booking_staff_pin');
    delete_option('maca_menulist_booking_staff_slug');
    update_option('maca_menulist_booking_staff_pin_removed', MACA_MENULIST_VERSION);

    if (function_exists('maca_menulist_flush_rewrite_rules')) {
        maca_menulist_flush_rewrite_rules();
    }
}

add_action('init', 'maca_menulist_remove_booking_staff_pin_options', 11);

/**
 * Register booking host role and grant caps to administrators.
 *
 * @return void
 */
function maca_menulist_register_booking_host_role() {
    $caps = array(
        'read'                                    => true,
        MACA_MENULIST_CAP_MANAGE_BOOKINGS         => true,
        MACA_MENULIST_CAP_EDIT_BOOKINGS           => true,
        MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE => true,
    );

    $role = get_role(MACA_MENULIST_BOOKING_HOST_ROLE);
    if (!$role) {
        add_role(
            MACA_MENULIST_BOOKING_HOST_ROLE,
            __('Booking host', 'maca-restu-pro'),
            $caps
        );
    } else {
        foreach ($caps as $cap => $grant) {
            $role->add_cap($cap, $grant);
        }
    }

    $admin = get_role('administrator');
    if ($admin) {
        $admin->add_cap(MACA_MENULIST_CAP_MANAGE_BOOKINGS, true);
        $admin->add_cap(MACA_MENULIST_CAP_EDIT_BOOKINGS, true);
        $admin->add_cap(MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE, true);
    }
}

/**
 * Ensure booking host role always has booking edit caps at runtime.
 *
 * @param array<string, bool> $allcaps All capabilities for the user.
 * @param array<string>       $caps    Requested capabilities.
 * @param array<mixed>        $args    Capability check args.
 * @param WP_User             $user    User object.
 * @return array<string, bool>
 */
function maca_menulist_booking_host_user_caps($allcaps, $caps, $args, $user) {
    if (!($user instanceof WP_User)) {
        return $allcaps;
    }

    if (!in_array(MACA_MENULIST_BOOKING_HOST_ROLE, (array) $user->roles, true)) {
        return $allcaps;
    }

    $allcaps[ MACA_MENULIST_CAP_MANAGE_BOOKINGS ]         = true;
    $allcaps[ MACA_MENULIST_CAP_EDIT_BOOKINGS ]           = true;
    $allcaps[ MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE ] = true;

    return $allcaps;
}

add_filter('user_has_cap', 'maca_menulist_booking_host_user_caps', 10, 4);

/**
 * Grant booking caps to maca Restu demo users (role DB may predate cap additions).
 *
 * @param array<string, bool> $allcaps All capabilities for the user.
 * @param array<string>       $caps    Requested capabilities.
 * @param array<mixed>        $args    Capability check args.
 * @param WP_User             $user    User object.
 * @return array<string, bool>
 */
function maca_menulist_demo_user_booking_caps($allcaps, $caps, $args, $user) {
    if (!($user instanceof WP_User)) {
        return $allcaps;
    }

    if (!defined('MACA_MENULIST_DEMO_ROLE') || !in_array(MACA_MENULIST_DEMO_ROLE, (array) $user->roles, true)) {
        return $allcaps;
    }

    if (empty($allcaps[ MACA_MENULIST_CAP_MANAGE ])) {
        return $allcaps;
    }

    $allcaps[ MACA_MENULIST_CAP_MANAGE_BOOKINGS ]         = true;
    $allcaps[ MACA_MENULIST_CAP_EDIT_BOOKINGS ]           = true;
    $allcaps[ MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE ] = true;

    return $allcaps;
}

add_filter('user_has_cap', 'maca_menulist_demo_user_booking_caps', 10, 4);

/**
 * Can the current user change, move, cancel, or delete bookings (and use floor tools)?
 *
 * @return bool
 */
function maca_menulist_user_can_edit_bookings() {
    return (function_exists('maca_menulist_user_can_manage_plugin') && maca_menulist_user_can_manage_plugin())
        || current_user_can('manage_options')
        || current_user_can(MACA_MENULIST_CAP_EDIT_BOOKINGS)
        || current_user_can(MACA_MENULIST_CAP_MANAGE_BOOKINGS);
}

/**
 * Can the current user manage table reservations in WP admin?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_bookings() {
    return (function_exists('maca_menulist_user_can_manage_plugin') && maca_menulist_user_can_manage_plugin())
        || current_user_can('manage_options')
        || current_user_can(MACA_MENULIST_CAP_MANAGE_BOOKINGS);
}

/**
 * Can the current user edit opening hours and booking exceptions?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_booking_schedule() {
    return (function_exists('maca_menulist_user_can_manage_plugin') && maca_menulist_user_can_manage_plugin())
        || current_user_can('manage_options')
        || current_user_can(MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE);
}

/**
 * Logged-in user with booking caps but without full plugin admin.
 *
 * @return bool
 */
function maca_menulist_user_is_booking_host_only() {
    if (!is_user_logged_in()) {
        return false;
    }

    if (current_user_can('manage_options')) {
        return false;
    }

    if (function_exists('maca_menulist_user_is_demo_manager') && maca_menulist_user_is_demo_manager()) {
        return false;
    }

    return maca_menulist_user_can_manage_bookings() || maca_menulist_user_can_manage_booking_schedule();
}

/**
 * Verify logged-in user + nonce for booking AJAX.
 *
 * @param string $nonce_action Nonce action name.
 * @return bool
 */
function maca_menulist_verify_booking_management_ajax($nonce_action = 'maca_admin_booking_floor') {
    if (!maca_menulist_pro_has_feature('table_bookings') || !maca_menulist_user_can_edit_bookings()) {
        return false;
    }

    $nonce = isset($_POST['nonce']) ? sanitize_text_field(wp_unslash($_POST['nonce'])) : '';

    return $nonce !== '' && wp_verify_nonce($nonce, $nonce_action);
}

/**
 * Send JSON error when booking management is not authorized.
 *
 * @return void
 */
function maca_menulist_booking_management_ajax_deny() {
    wp_send_json_error(array('message' => __('Unauthorized.', 'maca-restu-pro')));
}
