<?php

namespace Licenzo\Hooks\Handler\Frontend;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

use Licenzo\Framework\Asset\Vite;
use Licenzo\Framework\Foundation\Application;
use Licenzo\Helper\Settings;

class LicenzoCustomerPortalHandler
{
    private $app;
    public function __construct(Application $app)
    {
        $this->app = $app;
    }

    public function handle()
    {
        $this->enqueueAssets();
        return '<div id="licenzo-customers-app">Loading...</div>';
    }

    private function enqueueAssets()
    {
        Vite::enqueue(
            'licenzo-customers-app',
            'resources/Customers/app.js',
            [],
            null,
            true
        );

        wp_localize_script('licenzo-customers-app', 'licenzo_customer_app_', [
            'api_url'    => rest_url('licenzo/v1/'),
            'nonce'      => wp_create_nonce('wp_rest'),
            'version'    => $this->app->version(),
            'currency'   => static::resolveStoreCurrency(),
            'locale'     => str_replace('_', '-', get_locale()),
        ]);
    }

    private static function resolveStoreCurrency(): string
    {
        $provider = Settings::getGeneralSettings()['commerce_provider'] ?? 'woocommerce';

        if ($provider === 'edd' && function_exists('edd_get_currency')) {
            return edd_get_currency();
        }

        if (function_exists('get_woocommerce_currency')) {
            return get_woocommerce_currency();
        }

        return 'USD';
    }
}
