<?php

if (!defined('ABSPATH')) {
    exit;
}

use WPQT\Asset\AssetRepository;
use WPQT\Location\LocationService;
use WPQT\Nonce\NonceService;
use WPQT\Pipeline\PipelineRepository;
use WPQT\Settings\SettingRepository;
use WPQT\Time\TimeRepository;
use WPQT\User\UserRepository;

add_action('wp_enqueue_scripts', 'wpqt_enqueue_user_public_page');
if (!function_exists('wpqt_enqueue_user_public_page')) {
    function wpqt_enqueue_user_public_page()
    {
        $locationService = new LocationService();
        $timeRepository = new TimeRepository();

        if (!$locationService->isWPQTPublicUserPage()) {
            return;
        }

        $pipelineRepo = new PipelineRepository();
        $userRepo = new UserRepository();
        $customUserPageStyles = SettingRepository::getUserPageCustomStyles();

        $build_asset = AssetRepository::getWPQTScriptBildAssets();
        $vendors_asset = AssetRepository::getWPQTVendorScriptBildAssets();
        $dependencies = AssetRepository::getWPQTScriptDependencies();

        wp_enqueue_style('wpqt-tailwind', WP_QUICKTASKER_PLUGIN_FOLDER_URL . 'build/tailwind.css', [], $build_asset['version']);
        wp_enqueue_script('wpqt-vendors', WP_QUICKTASKER_PLUGIN_FOLDER_URL . 'build/vendors.js', [], $vendors_asset['version'], true);
        wp_enqueue_script('wpqt-script', WP_QUICKTASKER_PLUGIN_FOLDER_URL . 'build/userApp.js', $dependencies, $build_asset['version'], true);

        wp_localize_script('wpqt-script', 'wpqt_user', [
            'userApiNonce' => NonceService::createNonce(WPQT_USER_API_NONCE),
            'siteURL'      => site_url(),
            'pluginURL'    => WP_QUICKTASKER_PLUGIN_FOLDER_URL,
            'timezone'     => $timeRepository->getWPTimezone(),
        ]);

        wp_enqueue_style('wpqt-user-page-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;700&display=swap', [], null);

        // Set script translations
        wp_set_script_translations('wpqt-script', 'quicktasker', WP_QUICKTASKER_PLUGIN_FOLDER_DIR . 'languages/json');

        wp_add_inline_style('wpqt-tailwind', '
			html {
				margin-top: 0 !important;
			}
			#wpadminbar {
				display: none;
			}
		');

        if ($customUserPageStyles) {
            wp_add_inline_style('wpqt-tailwind', wp_strip_all_tags($customUserPageStyles));
        }
    }
}
