<?php
/**
 * maca Restu manager and demo user capabilities.
 *
 * @package Maca_Menu_List
 */

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

if (!defined('MACA_MENULIST_CAP_MANAGE')) {
    define('MACA_MENULIST_CAP_MANAGE', 'maca_manage_menulist');
}

if (!defined('MACA_MENULIST_CAP_MANAGE_SECRETS')) {
    define('MACA_MENULIST_CAP_MANAGE_SECRETS', 'maca_manage_menulist_secrets');
}

if (!defined('MACA_MENULIST_DEMO_ROLE')) {
    define('MACA_MENULIST_DEMO_ROLE', 'maca_menulist_demo');
}

/**
 * Capability required for maca Restu admin menus and saves.
 *
 * @return string
 */
function maca_menulist_admin_required_cap() {
    return MACA_MENULIST_CAP_MANAGE;
}

/**
 * Top-level maca Restu admin menu slug.
 *
 * @return string
 */
function maca_menulist_admin_menu_slug() {
    return 'maca-restu';
}

/**
 * Whether an admin page slug belongs to maca Restu.
 *
 * @param string $page Admin page slug; empty uses the current request.
 * @return bool
 */
function maca_menulist_is_maca_restu_admin_page($page = '') {
    if ($page === '') {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
    }

    return $page !== '' && strpos($page, 'maca-restu') === 0;
}

/**
 * Whether the current admin request targets a maca Restu screen.
 *
 * @return bool
 */
function maca_menulist_is_maca_restu_admin_screen() {
    if (maca_menulist_is_maca_restu_admin_page()) {
        return true;
    }

    if (!function_exists('get_current_screen')) {
        return false;
    }

    $screen = get_current_screen();

    return $screen && is_string($screen->id) && strpos($screen->id, 'maca-restu') !== false;
}

/**
 * Register demo role and grant maca Restu caps to administrators.
 *
 * @return void
 */
function maca_menulist_register_menulist_user_access() {
    $demo_caps = array(
        'read'                     => true,
        MACA_MENULIST_CAP_MANAGE => true,
    );

    if (defined('MACA_MENULIST_CAP_MANAGE_BOOKINGS')) {
        $demo_caps[ MACA_MENULIST_CAP_MANAGE_BOOKINGS ]         = true;
        $demo_caps[ MACA_MENULIST_CAP_EDIT_BOOKINGS ]           = true;
        $demo_caps[ MACA_MENULIST_CAP_MANAGE_BOOKING_SCHEDULE ] = true;
    }

    $role = get_role(MACA_MENULIST_DEMO_ROLE);
    if (!$role) {
        add_role(
            MACA_MENULIST_DEMO_ROLE,
            __('maca Restu demo', 'maca-restu-pro'),
            $demo_caps
        );
    } else {
        foreach ($demo_caps as $cap => $grant) {
            $role->add_cap($cap, $grant);
        }
    }

    $admin = get_role('administrator');
    if ($admin) {
        $admin->add_cap(MACA_MENULIST_CAP_MANAGE, true);
        $admin->add_cap(MACA_MENULIST_CAP_MANAGE_SECRETS, true);
    }
}

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

/**
 * Can the current user manage maca Restu in wp-admin?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_plugin() {
    return current_user_can('manage_options') || current_user_can(MACA_MENULIST_CAP_MANAGE);
}

/**
 * Can the current user change secrets, license keys, and payment credentials?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_secrets() {
    return current_user_can('manage_options') || current_user_can(MACA_MENULIST_CAP_MANAGE_SECRETS);
}

/**
 * Logged-in maca Restu demo user without full WordPress admin.
 *
 * @return bool
 */
function maca_menulist_user_is_demo_manager() {
    if (!is_user_logged_in()) {
        return false;
    }

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

    return current_user_can(MACA_MENULIST_CAP_MANAGE);
}

/**
 * Admin page slugs demo users must not open.
 *
 * @return array<int, string>
 */
function maca_menulist_demo_denied_admin_pages() {
    return array(
        'maca-restu-backup',
        'maca-restu-subscription',
        'maca-restu-legal',
    );
}

/**
 * maca Restu admin pages demo users may open but not change (view only).
 *
 * @return array<int, string>
 */
function maca_menulist_demo_readonly_admin_pages() {
    return array(
        'maca-restu-payments',
        'maca-restu-payment-history',
        'maca-restu-dish-csv',
        'maca-restu-import-pdf',
    );
}

/**
 * Whether the current (or given) maca Restu admin page is read-only for demo users.
 *
 * @param string $page Admin page slug; empty uses the current request.
 * @return bool
 */
function maca_menulist_admin_is_demo_readonly_page($page = '') {
    if (!maca_menulist_user_is_demo_manager()) {
        return false;
    }

    if ($page === '') {
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : '';
    }

    return in_array($page, maca_menulist_demo_readonly_admin_pages(), true);
}

/**
 * Can the current user change payment settings?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_payments_settings() {
    return maca_menulist_user_can_manage_secrets();
}

/**
 * Can the current user import or export menu data?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_import_export() {
    return maca_menulist_user_can_manage_secrets();
}

/**
 * Can the current user connect Meta and publish news/events to Facebook/Instagram?
 *
 * @return bool
 */
function maca_menulist_user_can_manage_info_hub_social() {
    return maca_menulist_user_can_manage_secrets();
}

/**
 * Extra CSS class for demo read-only admin screens.
 *
 * @return string
 */
function maca_menulist_admin_demo_readonly_wrap_class() {
    return maca_menulist_admin_is_demo_readonly_page() ? ' maca-demo-readonly-wrap' : '';
}

/**
 * Warning notice on demo read-only admin screens.
 *
 * @return void
 */
function maca_menulist_render_admin_demo_readonly_notice() {
    if (!maca_menulist_admin_is_demo_readonly_page()) {
        return;
    }

    echo '<div class="notice notice-warning"><p>';
    esc_html_e(
        'Read-only for demo users: you can view this page but cannot save settings, import, or export.',
        'maca-restu-pro'
    );
    echo '</p></div>';
}
