<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- False positive: locally-scoped variables (template view-vars via TemplateLoader::render() extract+include within a method, or closure locals), not plugin globals.

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

if ( defined( 'BOOKARO_PLUGIN_DIR' ) ) {
    // Defer the integration-mode check to after_setup_theme. MyListing
    // registers itself as a theme — which loads AFTER plugins_loaded —
    // so bookaro_is_mylisting_active() returns false if we ask at
    // plugins_loaded time. With integration_mode left at the default
    // 'auto', that earlier-than-it-should-be check resolved to 'generic'
    // and mylisting.php never got required, leaving listing-page booking
    // buttons rendered but unwired (no flatpickr / booking.js enqueue).
    // Priority 5 so MyListing-aware filters are registered before any
    // default-priority hook that might query them.
    add_action( 'after_setup_theme', static function (): void {
        $load_mylisting = true;
        if ( function_exists( 'bookaro_get_integration_mode' ) ) {
            $load_mylisting = ( 'mylisting' === bookaro_get_integration_mode() );
        }
        if ( $load_mylisting ) {
            require_once BOOKARO_PLUGIN_DIR . 'includes/mylisting.php';
        }
    }, 5 );
}

add_filter( 'bookaro_feature_enabled', 'bookaro_lite_feature_enabled', 5, 2 );
add_filter( 'bookaro_frontend_features', 'bookaro_lite_frontend_features', 5, 1 );
add_filter( 'bookaro_provider_limit', 'bookaro_lite_provider_limit', 5 );
add_filter( 'bookaro_booking_limit', 'bookaro_lite_booking_limit', 5 );

function bookaro_lite_feature_enabled( bool $enabled, string $feature ): bool {
    $feature = sanitize_key( $feature );
    $pro_features = [
        'gcal',
        'ical',
        'messages',
        'sms',
        'payments',
        'webhooks',
        'reports',
        'exports',
    ];

    if ( in_array( $feature, $pro_features, true ) ) {
        return false;
    }

    if ( 'mylisting' === $feature ) {
        if ( function_exists( 'bookaro_get_integration_mode' ) ) {
            return 'mylisting' === bookaro_get_integration_mode();
        }
        return true;
    }

    return $enabled;
}

function bookaro_lite_frontend_features( array $features ): array {
    $features['gcal'] = false;
    $features['ical'] = false;
    return $features;
}

function bookaro_lite_provider_limit( int $limit ): int {
    return $limit;
}

function bookaro_lite_booking_limit( int $limit ): int {
    return $limit;
}
