<?php

namespace WPEventGenius\Common\Services;

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

class StyleService {
    /**
     * Track which styles have been enqueued
     * @var array
     */
    private $enqueued_styles = [];

    /**
     * Constructor
     */
    public function __construct() {}

    /**
     * Initialize the service
     */
    public function init_hooks() {
        // Register handles used by register_block_type( editor_style / style ) before blocks register (init priority 10).
        add_action( 'init', array( $this, 'register_dynamic_block_style_handles' ), 5 );

        // Front-end styles
        add_action('wp_enqueue_scripts', [$this, 'register_styles'], 5);
        
        // Admin styles
        add_action('admin_enqueue_scripts', [$this, 'register_admin_styles'], 5);
    }

    /**
     * Register stylesheet handles referenced by dynamic blocks so editor_style/style point at real CSS,
     * not script handles. Avoids calling wp_enqueue_style during block registration (wrong hook / global load).
     */
    public function register_dynamic_block_style_handles() {
        // Front: production uses handle aliases so blocks do not load duplicate CSS alongside evge.min.css.
        $this->register_core_styles( false );
        $this->register_feature_styles( false );
        wp_register_style(
            'evge-blocks-shared',
            trailingslashit( EVGE_PLUGIN_URL ) . 'assets/css/admin/evge-blocks-shared.css',
            array(),
            EVGE_VERSION
        );
    }

    /**
     * Register all front-end styles
     */
    public function register_styles() {
        $dev_mode = defined('EVGE_DEV_MODE') && EVGE_DEV_MODE;

        // Always register core styles first (needed for dependencies)
        $this->register_core_styles( false );

        if ( $dev_mode ) {
            // Register feature styles after common styles
            $this->register_feature_styles( false );
        } else {
            // In production mode, enqueue minified CSS files (handles are aliases to this bundle; see register_feature_styles).
            wp_enqueue_style( 'evge-styles' );
        }

    }

    /**
     * Register all admin styles
     */
    public function register_admin_styles() {
        // Register common styles first (needed by admin common); always use source files in admin.
        $this->register_core_styles( true );
        
        // Register admin global styles
        $this->register_admin_global_styles();
        
        // Register other admin styles
        $this->register_admin_feature_styles();

        // Register pro admin styles if pro features are available
        $this->register_admin_pro_styles();

        // Register front-end feature styles (for admin pages that use front-end components)
        $this->register_feature_styles( true );
    }

    /**
     * Whether front-end styles should use the built bundle (dependency-only handles) instead of source files.
     */
    private function is_frontend_production_bundle() {
        return ! ( defined( 'EVGE_DEV_MODE' ) && EVGE_DEV_MODE );
    }

    /**
     * Register the main front-end dist bundle handle (evge.min.css).
     */
    private function register_frontend_dist_bundle_handle() {
        wp_register_style(
            'evge-styles',
            trailingslashit( EVGE_PLUGIN_URL ) . 'assets/css/dist/evge.min.css',
            array(),
            EVGE_VERSION
        );
    }

    /**
     * Register a style handle that only pulls in other handles (no extra stylesheet).
     *
     * @param string   $handle Handle name.
     * @param string[] $deps   Dependency handles.
     */
    private function register_style_alias( $handle, array $deps ) {
        wp_register_style( $handle, false, $deps, EVGE_VERSION );
    }

    /**
     * Register core styles that are always needed
     *
     * @param bool $force_source_files When true (e.g. admin), always register unminified source paths.
     */
    private function register_core_styles( $force_source_files = false ) {
        if ( ! $force_source_files && $this->is_frontend_production_bundle() ) {
            $this->register_frontend_dist_bundle_handle();
            $this->register_style_alias( 'evge_common', array( 'evge-styles' ) );
            return;
        }

        wp_register_style(
            'evge_common',
            trailingslashit( EVGE_PLUGIN_URL ) . 'assets/css/front-end/evge-common.css',
            [], // No dependencies - this is the base stylesheet
            EVGE_VERSION
        );
    }

    /**
     * Register feature-specific styles
     *
     * @param bool $force_source_files When true (e.g. admin), always register unminified source paths.
     */
    private function register_feature_styles( $force_source_files = false ) {
        if ( ! $force_source_files && $this->is_frontend_production_bundle() ) {
            $this->register_frontend_dist_bundle_handle();

            $this->register_style_alias( 'evge_registration_form', array( 'evge-styles' ) );
            $this->register_style_alias( 'evge_attendee_list', array( 'evge-styles' ) );

            if ( function_exists( 'evge_is_standard_tier' ) && evge_is_standard_tier() ) {
                $this->register_style_alias( 'evge_admin_check_in', array( 'evge-styles' ) );
            }

            $this->register_style_alias( 'evge_calendar', array( 'evge-styles' ) );
            $this->register_style_alias( 'evge_single_post', array( 'evge-styles' ) );

            if ( function_exists( 'evge_is_pro_tier' ) && evge_is_pro_tier() ) {
                $this->register_style_alias( 'evge_payments', array( 'evge-styles' ) );
                $this->register_style_alias( 'evge_additional_guests', array( 'evge-styles' ) );
            }

            if ( function_exists( 'evge_is_premium_tier' ) && evge_is_premium_tier() ) {
                wp_register_style(
                    'evge_bulk_registration',
                    trailingslashit( EVGE_PLUGIN_URL ) . 'assets/css/dist/evge-premium.min.css',
                    array( 'evge-styles' ),
                    EVGE_VERSION
                );
                wp_register_style(
                    'evge-premium-styles',
                    trailingslashit( EVGE_PLUGIN_URL ) . 'assets/css/dist/evge-premium.min.css',
                    array( 'evge-styles' ),
                    EVGE_VERSION
                );
            }

            $this->register_style_alias( 'evge-frontend-admin-notices', array( 'evge-styles' ) );
            return;
        }

        // Registration Form Feature
        wp_register_style(
            'evge_registration_form',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/evge-registration-form.css',
            array( 'evge_common' ),
            EVGE_VERSION
        );

        // Attendee List Feature
        wp_register_style(
            'evge_attendee_list',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/evge-attendee-list.css',
            ['evge_common'], // Explicitly depend on common styles
            EVGE_VERSION
        );

        // Admin Check In Feature (Standard tier only)
        if ( function_exists( 'evge_is_standard_tier' ) && evge_is_standard_tier() ) {
            wp_register_style(
                'evge_admin_check_in',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/pro/evge-admin-check-in.css',
                ['evge_common'], // Explicitly depend on common styles
                EVGE_VERSION
            );
        }

        // Calendar Feature
        wp_register_style(
            'evge_calendar',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/evge-calendar.css',
            ['evge_common'], // Explicitly depend on common styles
            EVGE_VERSION
        );

        // Event Feature
        wp_register_style(
            'evge_single_post',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/evge-single-post.css',
            ['evge_common'], // Explicitly depend on common styles
            EVGE_VERSION
        );

        // Pro Payments Feature (only register if Pro features are available)
        if (function_exists('evge_is_pro_tier') && evge_is_pro_tier()) {
            wp_register_style(
                'evge_payments',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/pro/evge-payments.css',
                ['evge_common'], // Explicitly depend on common styles
                EVGE_VERSION
            );
        }

        // Additional Guests Feature (register for Standard tier and above)
        // This style is used by AdditionalGuestsService when pro features are available
        if (function_exists('evge_is_pro_tier') && evge_is_pro_tier()) {
            wp_register_style(
                'evge_additional_guests',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/pro/evge-pro.css',
                ['evge_common'], // Explicitly depend on common styles
                EVGE_VERSION
            );
        }

        // Bulk Registration Feature (Premium tier only)
        if ( function_exists( 'evge_is_premium_tier' ) && evge_is_premium_tier() ) {
            $dev_mode = defined('EVGE_DEV_MODE') && EVGE_DEV_MODE;
            $css_path = $dev_mode 
                ? 'assets/css/front-end/premium/evge-bulk-registration.css'
                : 'assets/css/dist/evge-premium.min.css';
            
            wp_register_style(
                'evge_bulk_registration',
                trailingslashit(EVGE_PLUGIN_URL) . $css_path,
                ['evge_common'], // Explicitly depend on common styles
                EVGE_VERSION
            );
        }

        // Front-end Admin Notices Feature
        wp_register_style(
            'evge-frontend-admin-notices',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/front-end/evge-frontend-admin-notices.css',
            [],
            EVGE_VERSION
        );
    }

    /**
     * Register admin global styles that are always needed
     */
    private function register_admin_global_styles() {
        // Register Select2 CSS
        wp_register_style(
            'select2',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/lib/select2.min.css',
            [],
            '4.1.0'
        );
        
        // Admin global styles that all admin features depend on
        wp_register_style(
            'evge_admin_global',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-admin-global.css',
            [], // No dependencies - this is the base admin stylesheet
            EVGE_VERSION
        );

        // Enqueue admin global styles immediately to ensure they load first
        wp_enqueue_style('evge_admin_global');
        $this->enqueued_styles['evge_admin_global'] = true;
    }

    /**
     * Register admin feature-specific styles
     */
    private function register_admin_feature_styles() {
        // Admin Common Styles
        wp_register_style(
            'evge_admin_common',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-admin-common.css',
            ['evge_common'], // Depend on common styles (global utilities)
            EVGE_VERSION
        );

        // Calendar Builder Styles
        wp_register_style(
            'evge_calendar_builder',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-calendar-builder.css',
            ['evge_admin_common'], // Depend on admin global styles
            EVGE_VERSION
        );

        // Form Builder Styles
        wp_register_style(
            'evge_form_builder',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-form-builder.css',
            ['evge_admin_common'], // Depend on admin global styles
            EVGE_VERSION
        );

        // Custom Post Type Settings Styles
        wp_register_style(
            'evge_custom_post_type_settings',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-custom-post-type-settings.css',
            ['evge_admin_common'], // Depend on admin global styles
            EVGE_VERSION
        );

        wp_register_style(
            'evge-inline-cards-admin',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-inline-cards-admin.css',
            [],
            EVGE_VERSION
        );

        wp_register_style(
            'evge-registration-types-admin',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-registration-types-admin.css',
            ['evge-inline-cards-admin'],
            EVGE_VERSION
        );

        wp_register_style(
            'evge-registration-type-templates-settings',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-registration-type-templates-settings.css',
            array(),
            EVGE_VERSION
        );

        // Blocks Shared Styles
        wp_register_style(
            'evge_blocks_shared',
            trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/evge-blocks-shared.css',
            ['evge_admin_common'], // Depend on admin global styles
            EVGE_VERSION
        );

        // Series Management Styles (Pro tier only)
        if ( function_exists( 'evge_is_pro_tier' ) && evge_is_pro_tier() ) {
            wp_register_style(
                'evge_series_management',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/pro/evge-series-management.css',
                ['evge_admin_common', 'select2'], // Depend on admin global styles and Select2
                EVGE_VERSION
            );
        }

        // Scheduled Emails Admin Styles (Standard tier only)
        if (function_exists('evge_is_standard_tier') && evge_is_standard_tier()) {
            wp_register_style(
                'evge-scheduled-emails-admin',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/pro/evge-scheduled-emails-admin.css',
                ['evge-inline-cards-admin'],
                EVGE_VERSION
            );
        }
    }

    /**
     * Enqueue a style if it hasn't been enqueued yet
     * 
     * @param string $style_handle The style handle to enqueue
     */
    public function enqueue_style($style_handle) {
        if (!isset($this->enqueued_styles[$style_handle])) {
            wp_enqueue_style($style_handle);
            $this->enqueued_styles[$style_handle] = true;
        }
    }

    /**
     * Register admin pro styles if pro features are available
     */
    private function register_admin_pro_styles() {
        if (function_exists('evge_is_pro_tier') && evge_is_pro_tier()) {
            // Register pro admin CSS for all pro tiers
            wp_register_style(
                'evge_admin_pro',
                trailingslashit(EVGE_PLUGIN_URL) . 'assets/css/admin/pro/evge-admin-pro.css',
                ['evge_admin_common'], // Depend on admin common styles
                EVGE_VERSION
            );
            
            // Enqueue pro admin styles immediately
            wp_enqueue_style('evge_admin_pro');
            $this->enqueued_styles['evge_admin_pro'] = true;
        }
    }

    /**
     * Enqueue Pro payments styles if Pro features are available
     */
    public function enqueue_pro_payments_styles() {
        if (function_exists('evge_is_pro_tier') && evge_is_pro_tier()) {
            $this->enqueue_style('evge_payments');
        }
    }
} 