<?php
namespace WTC_CHECKOUT\Core;
defined('ABSPATH') || exit;
class AssetManager {
    public function __construct() {
        add_action('wp_enqueue_scripts', [$this, 'enqueue_global_assets'], 5);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_checkout_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_account_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_contract_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_coupons_assets']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_dashboard_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_global_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_field_layout_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_contracts_assets']);
    }
    public function enqueue_global_assets() {
        wp_enqueue_script(
            'wtc-frontend',
            WTC_PLUGIN_URL . 'assets/js/frontend-wtc.js',
            array(),
            WTC_VERSION,
            true
        );
    }
    public function enqueue_checkout_assets() {
        if (is_checkout()) {
            wp_enqueue_style('select2');
            wp_enqueue_script('select2');
            wp_enqueue_style(
                'wtc-styles', 
                WTC_PLUGIN_URL . 'assets/css/frontend-forms.css',
                array(),
                WTC_VERSION
            );
            wp_enqueue_script(
                'wtc-script-checkout',
                WTC_PLUGIN_URL . 'assets/js/frontend-forms-checkout.js',
                array('jquery', 'select2'),
                WTC_VERSION,
                true
            );
            $current_user_id = get_current_user_id();
            $user_data = array(
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('wtc-nonce'),
                'saved_phone' => '',
                'tckn_required' => get_option('wtc_tckn_required', 'yes'),
                'is_user_logged_in' => is_user_logged_in() ? '1' : '0',
                'billing_toggle_text' => __('Fatura adresini düzenle', 'wtc-checkout'),
                'corporate_names_required' => get_option('wtc_corporate_names_required', 'no'),
            );
            if ($current_user_id) {
                $billing_fields = ['invoice_type', 'tckn', 'tax_number', 'tax_office', 'first_name', 'last_name', 'company', 'country', 'state', 'city', 'address_1', 'address_2', 'postcode', 'phone'];
                $shipping_fields = ['first_name', 'last_name', 'company', 'country', 'state', 'city', 'address_1', 'address_2', 'postcode', 'name'];
                foreach ($billing_fields as $field) {
                    $user_data['saved_' . $field] = get_user_meta($current_user_id, 'billing_' . $field, true);
                }
                foreach ($shipping_fields as $field) {
                    $user_data['saved_shipping_' . $field] = get_user_meta($current_user_id, 'shipping_' . $field, true);
                }
                if (empty($user_data['saved_city'])) {
                    $base_country = WC()->countries->get_base_country();
                    $base_state = WC()->countries->get_base_state();
                    $user_data['saved_country'] = $user_data['saved_country'] ?: $base_country;
                    $user_data['saved_state'] = $user_data['saved_state'] ?: $base_state;
                }
                if (empty($user_data['saved_shipping_city'])) {
                    $base_country = WC()->countries->get_base_country();
                    $base_state = WC()->countries->get_base_state();
                    $user_data['saved_shipping_country'] = $user_data['saved_shipping_country'] ?: $base_country;
                    $user_data['saved_shipping_state'] = $user_data['saved_shipping_state'] ?: $base_state;
                }
                $country_code = get_user_meta($current_user_id, 'billing_country', true);
                $state_code = get_user_meta($current_user_id, 'billing_state', true);
                $countries = WC()->countries->get_countries();
                $states = WC()->countries->get_states($country_code);
                $user_data['saved_country_name'] = isset($countries[$country_code]) ? $countries[$country_code] : $country_code;
                $user_data['saved_state_name'] = isset($states[$state_code]) ? $states[$state_code] : $state_code;
            }
            wp_localize_script('wtc-script-checkout', 'wtc_params', $user_data);
        }
    }
    public function enqueue_account_assets() {
        if (is_account_page()) {
            wp_enqueue_style('select2');
            wp_enqueue_script('select2');
            wp_enqueue_style(
                'wtc-forms', 
                WTC_PLUGIN_URL . 'assets/css/frontend-forms.css',
                array(),
                WTC_VERSION
            );
            wp_enqueue_script(
                'wtc-my-account',
                WTC_PLUGIN_URL . 'assets/js/frontend-forms-account.js',
                array('jquery', 'select2'),
                WTC_VERSION,
                true
            );
            wp_localize_script('wtc-my-account', 'wtc_params', array(
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('wtc-nonce'),
                'tckn_required' => get_option('wtc_tckn_required', 'yes'),
                'corporate_names_required' => get_option('wtc_corporate_names_required', 'no'),
            ));
        }
    }
    public function enqueue_contract_assets() {
        if (get_option('wtc_contracts_enabled', 'no') !== 'yes') {
            return;
        }
        if (is_checkout() || is_view_order_page() || is_order_received_page()) {
            wp_enqueue_style(
                'wtc-contracts', 
                WTC_PLUGIN_URL . 'assets/css/frontend-contracts.css',
                array(),
                WTC_VERSION
            );
            wp_enqueue_script(
                'wtc-contracts', 
                WTC_PLUGIN_URL . 'assets/js/frontend-contracts.js',
                array('jquery'),
                WTC_VERSION,
                true
            );
            wp_localize_script('wtc-contracts', 'wtc_contracts_params', array(
                'debug' => defined('WP_DEBUG') && WP_DEBUG,
                'nonce' => wp_create_nonce('wtc-contracts'),
                'enabled' => get_option('wtc_contracts_enabled', 'no') === 'yes',
                'corporate_names_required' => get_option('wtc_corporate_names_required', 'no'),
            ));
        }
    }
    public function enqueue_dashboard_assets() {
        if (get_option('wtc_dashboard_enabled', 'no') !== 'yes') {
            return;
        }
        if (!is_account_page() || !is_user_logged_in()) {
            return;
        }
        wp_enqueue_style(
            'wtc-dashboard', 
            WTC_PLUGIN_URL . 'assets/css/frontend-dashboard.css',
            array(),
            WTC_VERSION
        );
        $custom_css_path = WP_CONTENT_DIR . '/uploads/wtc/dashboard-custom.css';
        if (file_exists($custom_css_path)) {
            wp_enqueue_style(
                'wtc-dashboard-custom',
                content_url('/uploads/wtc/dashboard-custom.css'),
                array('wtc-dashboard'),
                filemtime($custom_css_path)
            );
        }
        if (get_option('wtc_dashboard_chart_enabled', 'no') === 'yes') {
            wp_enqueue_script('chart-js', 'https://cdn.jsdelivr.net/npm/chart.js', array(), '4.4.0', true);
        }
        wp_enqueue_script(
            'wtc-dashboard', 
            WTC_PLUGIN_URL . 'assets/js/frontend-dashboard.js',
            array('jquery'),
            WTC_VERSION,
            true
        );
        $chart_data = ['labels' => [], 'values' => []];
        $chart_enabled = get_option('wtc_dashboard_chart_enabled', 'no') === 'yes';
        if ($chart_enabled) {
            $user_id = get_current_user_id();
            $period = get_option('wtc_dashboard_chart_period', '1_year');
            $chart_data = \WTC_CHECKOUT\Modules\Dashboard\Services\DashboardDataService::get_instance()->get_spending_chart_data($user_id, $period);
        }
        wp_localize_script('wtc-dashboard', 'wtc_dashboard_params', array(
            'ajax_url'    => admin_url('admin-ajax.php'),
            'nonce'       => wp_create_nonce('wtc_dashboard_nonce'),
            'theme_mode'  => get_option('wtc_dashboard_theme_mode', 'auto'),
            'chart_enabled' => $chart_enabled ? '1' : '0',
            'chart_labels'  => $chart_data['labels'],
            'chart_values'  => $chart_data['values']
        ));
    }
    public function enqueue_coupons_assets() {
        if (get_option('wtc_coupons_enabled', 'no') !== 'yes') {
            return;
        }
        if (!is_user_logged_in() || !is_account_page()) {
            return;
        }
        $coupon_endpoint = get_option('wtc_coupon_endpoint', 'kuponlarim');
        global $wp_query;
        $is_coupon_page = isset($wp_query->query_vars[$coupon_endpoint]);
        if (!$is_coupon_page) {
            return;
        }
        wp_enqueue_style(
            'wtc-coupons', 
            WTC_PLUGIN_URL . 'assets/css/frontend-coupons.css',
            array(),
            WTC_VERSION
        );
        wp_enqueue_script(
            'wtc-coupons', 
            WTC_PLUGIN_URL . 'assets/js/frontend-coupons.js',
            array('jquery'),
            WTC_VERSION,
            true
        );
    }
    public function enqueue_admin_global_assets($hook) {
        if (strpos($hook, 'wtc-') !== false || strpos($hook, 'wtc_page_') !== false) {
            wp_enqueue_style(
                'wtc-admin-unified', 
                WTC_PLUGIN_URL . 'assets/css/admin-unified.css',
                array(), 
                WTC_VERSION
            );
            if ($hook === 'wtc_page_wtc-dashboard') {
                wp_enqueue_style('wp-color-picker');
                wp_enqueue_script(
                    'wtc-admin-dashboard',
                    WTC_PLUGIN_URL . 'assets/js/admin-dashboard.js',
                    array('jquery', 'wp-color-picker'),
                    WTC_VERSION,
                    true
                );
            }
        }
    }
    public function enqueue_admin_field_layout_assets($hook) {
        if ($hook === 'wtc_page_wtc-layouts') {
            wp_enqueue_script('jquery-ui-sortable');
            wp_enqueue_script(
                'wtc-admin-forms',
                WTC_PLUGIN_URL . 'assets/js/admin-forms.js',
                array('jquery', 'jquery-ui-sortable'),
                WTC_VERSION,
                true
            );
        }
    }
    public function enqueue_admin_contracts_assets($hook) {
        if ($hook === 'wtc_page_wtc-contracts' || strpos($hook, 'wtc-contracts') !== false) {
            wp_enqueue_script(
                'wtc-admin-contracts',
                WTC_PLUGIN_URL . 'assets/js/admin-contracts.js',
                array('jquery', 'wp-editor'),
                WTC_VERSION,
                true
            );
        }
    }
}