<?php
if (!defined('ABSPATH')) {
    exit;
}

class Manfiet_Expense_Tracker_Elementor {
    public function __construct() {
        // Register hooks if Elementor is active and the widget setting is enabled
        if (class_exists('\Elementor\Plugin') && get_option('manfiet_enable_elementor_widgets', 1)) {
            add_action('elementor/elements/categories_registered', [$this, 'register_widget_category']);
            add_action('elementor/widgets/register', [$this, 'register_widgets']);
            add_action('wp_enqueue_scripts', [$this, 'register_frontend_assets']);
        }
    }

    /**
     * Register a custom category for Manfi Expense Tracker widgets.
     */
    public function register_widget_category($elements_manager) {
        $elements_manager->add_category(
            'manfi-expense-tracker',
            [
                'title' => esc_html__('Manfi Expense Tracker', 'manfi-expense-tracker'),
                'icon'  => 'fa fa-plug',
            ]
        );
    }

    /**
     * Register Manfi Expense Tracker Elementor widgets.
     */
    public function register_widgets($widgets_manager) {
        // Register widgets with the widgets manager (automatically loaded via SPL Autoloader)
        $widgets_manager->register(new \Manfiet_Expense_Tracker_Widget_Expense_Entry());
        $widgets_manager->register(new \Manfiet_Expense_Tracker_Widget_Expense_Dashboard());
        $widgets_manager->register(new \Manfiet_Expense_Tracker_Widget_Budget_Showcase());
    }

    /**
     * Register all frontend assets required for widgets so they can be listed as dependencies.
     */
    public function register_frontend_assets() {
        wp_register_style(
            'manfiet-expense-frontend-style',
            MANFIET_EXPENSE_TRACKER_PLUGIN_URL . 'assets/css/frontend.css',
            [],
            MANFIET_EXPENSE_TRACKER_VERSION
        );

        wp_register_script(
            'manfiet-frontend-app',
            MANFIET_EXPENSE_TRACKER_PLUGIN_URL . 'assets/js/admin.js',
            ['wp-element'],
            MANFIET_EXPENSE_TRACKER_VERSION,
            true
        );

        wp_localize_script('manfiet-frontend-app', 'manfietExpenseTrackerData', [
            'restBase' => esc_url_raw(rest_url('manfi-expense-tracker/v1/')),
            'restNonce' => wp_create_nonce('wp_rest'),
            'canManageExpenses' => current_user_can('manage_expenses') || current_user_can('manage_options'),
            'pageView' => 'frontend',
            'expensesPerPage' => (int) get_option('manfiet_expenses_per_page', 20),
            'currencySymbol' => class_exists('Manfiet_Expense_Tracker_Settings') ? Manfiet_Expense_Tracker_Settings::get_current_currency_symbol() : '$',
            'currencyCode' => get_option('manfiet_currency', 'USD'),
        ]);
    }

    /**
     * Enqueue all frontend assets required for widgets.
     */
    public static function enqueue_frontend_assets() {
        // Enqueue the registered style and script
        wp_enqueue_style('manfiet-expense-frontend-style');
        wp_enqueue_script('manfiet-frontend-app');
    }
}
