<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
class Enqueue_Assets
{

    public function __construct()
    {
        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
    }

    public function enqueue_frontend_assets()
    {
        // Tailwind CSS
        wp_enqueue_style('pawfect-style', PAWFECT_THEME_URL . '/dist/output.css', [], PAWFECT_THEME_VERSION, 'all');

        // AOS Animation Library (local) - JS only (CSS is built into Tailwind output.css)
        wp_enqueue_script('pawfect-aos', PAWFECT_THEME_URL . '/assets/js/aos.js', [], PAWFECT_THEME_VERSION, true);

        // Main JS - Depends on AOS
        wp_enqueue_script('pawfect-main', PAWFECT_THEME_URL . '/assets/js/main.js', ['pawfect-aos'], PAWFECT_THEME_VERSION, true);

        /**
         * External libraries bundled locally for WordPress.org compliance.
         * All CDN dependencies have been downloaded and stored in assets/vendor/
         */

        // SwiperJS - Bundled locally, only loaded where needed (saves 532 KB on other pages)
        if ( is_front_page() || is_page_template() || ( function_exists( 'is_product' ) && is_product() ) ) {
            wp_enqueue_style('pawfect-swiper', get_template_directory_uri() . '/assets/vendor/swiper/swiper-bundle.min.css', [], '11.0.0');
            wp_enqueue_script('pawfect-swiper', get_template_directory_uri() . '/assets/vendor/swiper/swiper-bundle.min.js', [], '11.0.0', true);
        }

        // Font Awesome - Bundled locally
        wp_enqueue_style('pawfect-font-awesome', get_template_directory_uri() . '/assets/vendor/font-awesome/css/all.min.css', [], '6.0.0-beta3');

        // Phosphor Icons - Bundled locally
        wp_enqueue_style('pawfect-phosphor-icons', get_template_directory_uri() . '/assets/vendor/phosphor-icons/style.css', [], '2.0.3');

        // Google Fonts for single post design only (locally hosted via WPTT Webfont Loader for GDPR compliance)
        if ( is_single() ) {
            wp_enqueue_style('pawfect-google-fonts', wptt_get_webfont_url('https://fonts.googleapis.com/css2?family=Lato:wght@400;700&family=Playfair+Display:wght@700&display=swap'), [], null);
        }

        // Load selected fonts from customizer
        $this->enqueue_customizer_fonts();

        // Comment reply script
        if (is_singular() && comments_open() && get_option('thread_comments')) {
            wp_enqueue_script('comment-reply');
        }

        // Custom styles for single post
        if (is_single()) {
            wp_add_inline_style('pawfect-style', $this->get_single_post_styles());
        }

        // Keyboard accessibility: TwentyTwenty-style keyboard navigation.
        //
        // KEY FIX: Override visibility:hidden on dropdown menus.
        // visibility:hidden makes elements completely unfocusable by keyboard.
        // TwentyTwenty avoids this by using off-screen positioning (right:9999rem)
        // instead of visibility:hidden. We achieve the same by forcing
        // visibility:visible — the menu stays invisible via opacity:0 and
        // non-clickable via pointer-events:none, but keyboard Tab/Shift+Tab
        // can now reach the submenu links.
        wp_add_inline_style('pawfect-style', '
            .pawfect-dropdown-menu {
                visibility: visible !important;
            }
            /* Show dropdown on HOVER (mouse) */
            .pawfect-group:hover > .pawfect-dropdown-menu,
            li.pawfect-has-dropdown:hover > .pawfect-dropdown-menu,
            /* Show dropdown on KEYBOARD FOCUS */
            .pawfect-has-dropdown.focus > .pawfect-dropdown-menu,
            .pawfect-has-dropdown:focus-within > .pawfect-dropdown-menu {
                opacity: 1 !important;
                pointer-events: auto !important;
                transform: translateY(0) translateX(0) !important;
            }
            /* Submenu items with children: flex row so link + arrow stay inline */
            .pawfect-submenu-item.pawfect-has-dropdown {
                display: flex !important;
                align-items: center !important;
                justify-content: space-between !important;
            }
            .pawfect-submenu-item.pawfect-has-dropdown > a {
                flex: 1;
            }
            /* Dropdown toggle button styling */
            .pawfect-dropdown-toggle {
                color: inherit;
                line-height: 1;
            }
            /* Fix arrow toggles inside submenu items so they don\'t overlap the label text */
            .pawfect-submenu-item.pawfect-has-dropdown > a {
                padding-right: 2.5rem !important; /* give space for the arrow */
            }
        ');

        // Contact Form 7 and WPForms Custom Styling
        wp_add_inline_style('pawfect-style', '
            /* Contact Form Styling */
            .wpcf7-form label,
            .wpforms-container .wpforms-field-label {
                color: #374151 !important;
                font-weight: 600;
            }
            .wpcf7-form-control.wpcf7-text,
            .wpcf7-form-control.wpcf7-textarea,
            .wpforms-container .wpforms-field input,
            .wpforms-container .wpforms-field textarea {
                width: 100% !important;
                padding: 0.75rem 1rem !important;
                border-radius: 0.5rem !important;
                background-color: #f9fafb !important;
                border: 1px solid #e5e7eb !important;
                transition: all 0.3s ease !important;
                color: #374151 !important;
            }
            .wpcf7-form-control.wpcf7-text:focus,
            .wpcf7-form-control.wpcf7-textarea:focus,
            .wpforms-container .wpforms-field input:focus,
            .wpforms-container .wpforms-field textarea:focus {
                border-color: var(--pawfect-primary) !important;
                box-shadow: 0 0 0 2px var(--pawfect-primary) !important;
                outline: none !important;
            }
            .wpcf7-submit,
            .wpforms-container .wpforms-submit {
                background-color: var(--pawfect-primary) !important;
                color: #ffffff !important;
                padding: 0.75rem 2rem !important;
                border-radius: 0.5rem !important;
                font-weight: 700 !important;
                cursor: pointer !important;
                border: none !important;
                transition: background-color 0.3s ease !important;
            }
            .wpcf7-submit:hover,
            .wpforms-container .wpforms-submit:hover {
                background-color: #111827 !important; /* pawfect-dark */
            }
            /* CF7 error validation tweaks */
            .wpcf7-not-valid-tip {
                font-size: 0.875rem;
                margin-top: 0.25rem;
                color: #dc2626;
            }
            .wpcf7 form.invalid .wpcf7-response-output,
            .wpcf7 form.unaccepted .wpcf7-response-output,
            .wpcf7 form.payment-required .wpcf7-response-output {
                border-color: #dc2626 !important;
            }
            .wpcf7 form.sent .wpcf7-response-output {
                border-color: #16a34a !important;
            }
        ');

        wp_add_inline_style('pawfect-style', '
            /* Prevent category overflow on blog cards */
            .pawfect-post-card .pawfect-post-meta {
                overflow: hidden !important;
                word-break: break-word !important;
                overflow-wrap: break-word !important;
                max-width: 100% !important;
            }
            .pawfect-post-card {
                overflow: hidden !important;
                min-width: 0 !important;
                max-width: 100% !important;
            }
            /* Grid children need min-width: 0 to respect overflow */
            .pawfect-grid > * {
                min-width: 0 !important;
            }
        ');

        // Content link underlines — required by WordPress.org theme review.
        // Links within the_content(), the_excerpt(), comments, text widgets,
        // and custom options with large blocks of text must be underlined.
        wp_add_inline_style('pawfect-style', '
            /* Post content (single posts/pages) */
            .pawfect-post-content a,
            .entry-content a,
            .pawfect-single-content a,
            .pawfect-content a,
            .pawfect-prose a,
            .wp-block-post-content a,

            /* Excerpts on blog listing / archive pages */
            .pawfect-post-card .pawfect-post-excerpt a,
            .pawfect-post-card p a,
            .pawfect-blog-main article p a,
            .entry-summary a,

            /* Comments */
            .comment-content a,
            .comments-area .comment-body a,

            /* Sidebar & Footer Widgets */
            .widget a,
            .widget_text a,
            .textwidget a,
            .widget_block a,
            .widget_recent_entries a,
            .widget_categories a,
            .widget_archive a,
            .widget_meta a,
            .widget_pages a,
            .widget_nav_menu a,
            .wp-block-latest-posts a,
            .pawfect-blog-sidebar a,
            .pawfect-footer-widgets a,
            footer .widget a,

            /* Blockquotes and other content */
            blockquote a,
            .wp-block-quote a,

            /* Page header meta (categories, post info) */
            .pawfect-page-header a,
            .pawfect-page-header-heading a,

            /* Blog card categories and meta */
            .pawfect-post-card a,
            .pawfect-post-categories a {
                text-decoration: underline !important;
            }

            /* Remove underline on hover for better UX */
            .pawfect-post-content a:hover,
            .entry-content a:hover,
            .comment-content a:hover,
            .widget a:hover,
            .widget_text a:hover,
            .textwidget a:hover,
            .widget_block a:hover,
            .pawfect-blog-sidebar a:hover,
            .pawfect-footer-widgets a:hover,
            footer .widget a:hover,
            blockquote a:hover,
            .entry-summary a:hover,
            .pawfect-post-card p a:hover {
                text-decoration: none !important;
            }

            /* Exclude navigation, buttons, and UI elements from underline */
            nav a,
            .pawfect-btn,
            .button,
            .wp-block-button a,
            .pawfect-header a,
            .pawfect-logo a,
            .pawfect-menu a,
            .pawfect-nav a,
            .pawfect-social a,
            .pawfect-pagination a,
            .page-numbers a,
            /* Breadcrumbs and post card titles should not be underlined */
            .pawfect-breadcrumbs a,
            .pawfect-post-card h2 a,
            .pawfect-post-card h3 a,
            .pawfect-post-card .pawfect-read-more,
            .pawfect-read-more,
            a.pawfect-read-more {
                text-decoration: none !important;
            }
        ');

        // Keyboard focus visibility — WordPress.org accessibility requirement.
        // Override all outline:none with a visible focus indicator.
        // This ensures keyboard users always see where focus is.
        wp_add_inline_style('pawfect-style', '
            /* Global focus indicator for all interactive elements */
            a:focus-visible,
            button:focus-visible,
            input:focus-visible,
            select:focus-visible,
            textarea:focus-visible,
            [tabindex]:focus-visible,
            .pawfect-btn:focus-visible,
            .button:focus-visible,
            .wp-block-button a:focus-visible {
                outline: 2px solid var(--pawfect-primary, #D9A477) !important;
                outline-offset: 2px !important;
            }

            /* Ensure form inputs show focus ring even if outline:none was set */
            .woocommerce .form-row input.input-text:focus,
            .woocommerce .form-row textarea:focus,
            .woocommerce .form-row select:focus,
            .pawfect-input:focus {
                outline: none;
                box-shadow: 0 0 0 2px rgba(217, 164, 119, 0.4) !important;
                border-color: var(--pawfect-primary, #D9A477) !important;
            }

            /* Skip link visible on focus */
            .skip-link:focus {
                clip: auto !important;
                clip-path: none !important;
                height: auto !important;
                width: auto !important;
                position: fixed !important;
                top: 8px !important;
                left: 8px !important;
                z-index: 100000 !important;
                padding: 12px 24px !important;
                background: var(--pawfect-primary, #D9A477) !important;
                color: #fff !important;
                font-size: 14px !important;
                font-weight: 600 !important;
                text-decoration: none !important;
                border-radius: 4px !important;
                box-shadow: 0 4px 12px rgba(0,0,0,0.3) !important;
            }

            /* Respect prefers-reduced-motion */
            @media (prefers-reduced-motion: reduce) {
                *, *::before, *::after {
                    animation-duration: 0.01ms !important;
                    animation-iteration-count: 1 !important;
                    transition-duration: 0.01ms !important;
                }
            }
        ');

        // Long title overflow protection — prevents horizontal scrollbar.
        wp_add_inline_style('pawfect-style', '
            .site-title,
            h1, h2, h3, h4, h5, h6,
            .pawfect-page-header h1,
            .entry-title,
            .pawfect-post-title,
            .woocommerce div.product .product_title {
                overflow-wrap: break-word;
                word-break: break-word;
                hyphens: auto;
            }
        ');
    }

    public function enqueue_admin_assets()
    {
        // Admin CSS
        wp_enqueue_style('pawfect-admin-style', PAWFECT_THEME_URL . '/inc/admin/css/admin.css');

        // Admin JS
        wp_enqueue_script('pawfect-admin-script', PAWFECT_THEME_URL . '/inc/admin/js/pawfect-admin-script.js', ['jquery'], PAWFECT_THEME_VERSION, true);
        wp_localize_script(
            'pawfect-admin-script',
            'pawfect_ajax_object',
        ['ajax_url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('pawfect_admin_nonce')]
        );
    }

    /**
     * Get custom styles for single post pages
     */
    private function get_single_post_styles()
    {
        return '
            body {
                font-family: "Lato", sans-serif;
                background-color: #F5F0EB;
                color: #333333;
            }

            /* Post content styling for better readability */
            .pawfect-post-content h2 {
                font-family: "Playfair Display", serif;
                font-size: 1.875rem; /* 30px */
                margin-top: 2rem;
                margin-bottom: 1rem;
                color: #333333;
            }

            .pawfect-post-content p {
                margin-bottom: 1.5rem;
                line-height: 1.7;
                color: #333333;
            }

            .pawfect-post-content blockquote {
                border-left: 4px solid var(--pawfect-primary, #D9A477);
                padding-left: 1.5rem;
                margin: 2rem 0;
                font-style: italic;
                color: #555555;
                font-size: 1.125rem;
            }

            .pawfect-post-content img {
                border-radius: 0.5rem;
                box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
                margin: 2rem 0;
            }

            /* Custom color variables */
            :root {
                --pawfect-background: #F5F0EB;
                --pawfect-accent: #D9A477;
                --pawfect-text-dark: #333333;
                --pawfect-text-light: #555555;
                --pawfect-footer-bg: #1E1E1E;
                --pawfect-border-color: #E0D8D1;
            }

            .pawfect-bg-background { background-color: var(--pawfect-background); }
            .pawfect-bg-accent { background-color: var(--pawfect-accent); }
            .pawfect-text-accent { color: var(--pawfect-accent); }
            .pawfect-text-text-dark { color: var(--pawfect-text-dark); }
            .pawfect-text-text-light { color: var(--pawfect-text-light); }
            .pawfect-bg-footer-bg { background-color: var(--pawfect-footer-bg); }
            .pawfect-border-border-color { border-color: var(--pawfect-border-color); }

            .pawfect-font-serif { font-family: "Playfair Display", serif; }
            .pawfect-font-sans { font-family: "Lato", sans-serif; }

            .hover\\:pawfect-text-accent:hover { color: var(--pawfect-accent); }
            .hover\\:pawfect-opacity-90:hover { opacity: 0.9; }
            .focus\\:pawfect-ring-accent:focus {
                --tw-ring-color: var(--pawfect-accent);
                box-shadow: 0 0 0 2px var(--pawfect-accent);
            }
        ';
    }

    /**
     * Enqueue fonts selected in customizer
     */
    private function enqueue_customizer_fonts()
    {
        // Get selected fonts from customizer
        $body_font = get_theme_mod('pawfect_body_font_family', 'Bricolage Grotesque');
        $heading_font = get_theme_mod('pawfect_heading_font_family', 'Bricolage Grotesque');
        $menu_font = get_theme_mod('pawfect_menu_font_family', 'Bricolage Grotesque');
        
        $h1_font = get_theme_mod('pawfect_h1_font_family', $heading_font);
        $h2_font = get_theme_mod('pawfect_h2_font_family', $heading_font);
        $h3_font = get_theme_mod('pawfect_h3_font_family', $heading_font);
        $h4_font = get_theme_mod('pawfect_h4_font_family', $heading_font);
        $h5_font = get_theme_mod('pawfect_h5_font_family', $heading_font);
        $h6_font = get_theme_mod('pawfect_h6_font_family', $heading_font);
        
        $button_font = get_theme_mod('pawfect_button_font_family', 'Bricolage Grotesque');
        $blog_title_font = get_theme_mod('pawfect_blog_title_font_family', 'Bricolage Grotesque');
        $page_title_font = get_theme_mod('pawfect_page_title_font_family', 'Bricolage Grotesque');
        $submenu_font = get_theme_mod('pawfect_submenu_font_family', 'Bricolage Grotesque');

        // Only load Google Fonts (skip system fonts)
        $google_fonts = ['Bricolage Grotesque', 'Inter', 'Poppins', 'Roboto', 'Open Sans', 'Lato', 'Montserrat', 'Source Sans Pro', 'Nunito', 'Raleway', 'Playfair Display', 'Merriweather', 'Oswald', 'PT Sans', 'Ubuntu', 'Libre Baskerville', 'Crimson Text', 'Work Sans', 'Fira Sans', 'DM Sans', 'Manrope', 'Space Grotesk', 'Plus Jakarta Sans'];

        // Build unique fonts array
        $unique_fonts = [];
        $selected_fonts = [
            $body_font, $heading_font, $menu_font,
            $h1_font, $h2_font, $h3_font, $h4_font, $h5_font, $h6_font,
            $button_font, $blog_title_font, $page_title_font, $submenu_font
        ];

        foreach ($selected_fonts as $font) {
            if (in_array($font, $google_fonts)) {
                $unique_fonts[$font] = true;
            }
        }

        // Always load default fonts for fallback
        $unique_fonts['Bricolage Grotesque'] = true;
        $unique_fonts['Inter'] = true;

        // Build font families array with proper encoding
        $font_families = [];
        foreach ($unique_fonts as $font => $value) {
            $encoded_font = str_replace(' ', '+', $font);
            $font_families[] = $encoded_font . ':wght@400;500;600;700';
        }

        // Load fonts if any Google Fonts are selected
        if (!empty($font_families)) {
            $fonts_url = 'https://fonts.googleapis.com/css2?family=' . implode('&family=', $font_families) . '&display=swap';
            wp_enqueue_style('pawfect-customizer-fonts', wptt_get_webfont_url($fonts_url), [], null);
        }


    }
}
