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

class Fusion_WooDoo {

    private static ?Fusion_WooDoo $instance = null;

    public static function instance(): Fusion_WooDoo {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        $this->load_includes();
        $this->init_hooks();
    }

    private function load_includes(): void {
        $includes = [
            'class-api-client.php',
            'class-admin-settings.php',
            'class-rest-endpoints.php',
            'class-webhooks.php',
            'class-my-account.php',
            'class-order-timeline.php',
            'class-returns.php',
        ];
        foreach ($includes as $file) {
            $path = FUSION_WOODOO_PLUGIN_DIR . 'includes/' . $file;
            if (file_exists($path)) {
                require_once $path;
            }
        }
    }

    private function init_hooks(): void {
        add_action('init', [$this, 'load_textdomain']);
        add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']);
        add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);

        // Instantiate components
        new Fusion_WooDoo_Admin_Settings();
        new Fusion_WooDoo_REST_Endpoints();
        new Fusion_WooDoo_Webhooks();
        new Fusion_WooDoo_My_Account();
        new Fusion_WooDoo_Order_Timeline();
        new Fusion_WooDoo_Returns();
    }

    public function load_textdomain(): void {
        load_plugin_textdomain(
            'fusion-woodoo',
            false,
            dirname(plugin_basename(FUSION_WOODOO_PLUGIN_DIR . 'fusion-woodoo.php')) . '/languages'
        );
    }

    public function enqueue_frontend_assets(): void {
        if (is_account_page() || is_woocommerce()) {
            wp_enqueue_style(
                'fusion-woodoo-my-account',
                FUSION_WOODOO_PLUGIN_URL . 'assets/css/my-account.css',
                [],
                FUSION_WOODOO_VERSION
            );
            wp_enqueue_script(
                'fusion-woodoo-my-account',
                FUSION_WOODOO_PLUGIN_URL . 'assets/js/my-account.js',
                ['jquery'],
                FUSION_WOODOO_VERSION,
                true
            );
            wp_localize_script('fusion-woodoo-my-account', 'fusionWooDoo', [
                'ajaxUrl' => admin_url('admin-ajax.php'),
                'nonce'   => wp_create_nonce('fusion_woodoo_nonce'),
            ]);
        }
    }

    public function enqueue_admin_assets(string $hook): void {
        if (strpos($hook, 'fusion-woodoo') === false) {
            return;
        }
        wp_enqueue_style(
            'fusion-woodoo-admin',
            FUSION_WOODOO_PLUGIN_URL . 'assets/css/admin.css',
            [],
            FUSION_WOODOO_VERSION
        );
        wp_enqueue_script(
            'fusion-woodoo-admin',
            FUSION_WOODOO_PLUGIN_URL . 'assets/js/admin.js',
            ['jquery'],
            FUSION_WOODOO_VERSION,
            true
        );
        wp_localize_script('fusion-woodoo-admin', 'fusionWooDooAdmin', [
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'nonce'   => wp_create_nonce('fusion_woodoo_admin_nonce'),
        ]);
    }
}
