<?php

namespace SmartSlotAppointmentScheduler;

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

if (!class_exists('\SmartSlotAppointmentScheduler\Enqueuer')) {
	/**
	 * Class to Enqueue the scripts , styles, admin style and admin scripts.
	 */
	class Enqueuer {

		public static function setup() {
			add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_scripts']);
			add_action('admin_enqueue_scripts', [__CLASS__, 'admin_enqueue_scripts']);
		}

		/**
		 * assets for main
		 */
		public static function enqueue_scripts() {
			$plugin_path = plugin_dir_path(__DIR__) . 'build';
			$plugin_url = plugin_dir_url(__DIR__) . 'build';

			$asset_file = $plugin_path . '/main.asset.php';

			if (!file_exists($asset_file)) {
				return;
			}

			$asset = require $asset_file;

			wp_enqueue_script(
				'book_an_appointment',
				$plugin_url . '/main.js',
				$asset['dependencies'],
				$asset['version'],
				true
			);

			wp_enqueue_style(
				'ssas_book_appointment',
				$plugin_url . '/main.css',
				[],
				filemtime($plugin_path . '/main.css')
			);

			wp_localize_script('book_an_appointment', 'ssasBookingRest', [
				'root'  => esc_url_raw(rest_url()),
				'nonce' => wp_create_nonce('wp_rest'),
			]);
		}

		/**
		 * assets for admin
		 */
		public static function admin_enqueue_scripts() {
			$plugin_path = plugin_dir_path(__DIR__) . 'build';
			$plugin_url = plugin_dir_url(__DIR__) . 'build';

			$asset_file = $plugin_path . '/admin.asset.php';

			if (!file_exists($asset_file)) {
				return;
			}

			$admin_asset = require $asset_file;

			// ✅ SweetAlert first
			wp_enqueue_script(
				'sweetalert2',
				plugins_url('../src/scripts/sweetalert2.js', __FILE__),
				['jquery'],
				filemtime(plugin_dir_path(__FILE__) . '../src/scripts/sweetalert2.js'),
				true
			);

			// ✅ Your script depends on SweetAlert
			wp_enqueue_script(
				'ssas_book_appointment_admin',
				$plugin_url . '/admin.js',
				array('sweetalert2', 'jquery'),
				$admin_asset['version'],
				true
			);

			wp_enqueue_style(
				'ssas_book_appointment_admin',
				$plugin_url . '/admin.css',
				[],
				filemtime($plugin_path . '/admin.css')
			);

			wp_localize_script('ssas_book_appointment_admin', 'ssasAppointmentStatus', [
				'ajaxurl' => admin_url('admin-ajax.php'),
				'nonce'   => wp_create_nonce('ssas_update_status_nonce'),
			]);

		}
	}

		add_action('init', ['\\SmartSlotAppointmentScheduler\\Enqueuer', 'setup'], 11);
}
