<?php
/**
 * Enqueue widget assets on frontend.
 *
 * @package BOOSAIAS_For_WooCommerce
 */

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

class BOOSAIAS_Widget_Loader {

	public function init() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_assets' ) );
	}

	public function enqueue_assets() {
		// Ensure default license key is set if missing
		$license_key = get_option( 'boosaias_license_key' );
		if ( ! $license_key ) {
			update_option( 'boosaias_license_key', 'WAI-TEST-0000-0000' );
			$license_key = 'WAI-TEST-0000-0000';
		}

		wp_enqueue_style(
			'boosaias-widget',
			BOOSAIAS_PLUGIN_URL . 'public/css/widget.css',
			array(),
			BOOSAIAS_VERSION
		);

		wp_enqueue_script(
			'boosaias-widget',
			BOOSAIAS_PLUGIN_URL . 'public/js/widget.js',
			array(),
			BOOSAIAS_VERSION,
			true
		);

		wp_localize_script(
			'boosaias-widget',
			'boosaiasConfig',
			array(
				'apiUrl'         => esc_url( rest_url( 'boosaias/v1' ) ),
				'nonce'          => wp_create_nonce( 'wp_rest' ),
				'welcomeMessage' => get_option( 'boosaias_welcome_message', __( 'Hello! How can I help?', 'boostanova-ai-shop-assistant' ) ),
				'widgetColor'    => get_option( 'boosaias_widget_color', '#2271b1' ),
				'language'       => get_option( 'boosaias_language', 'uk' ),
				'showBranding'   => (bool) get_option( 'boosaias_show_branding', 0 ),
			)
		);
	}
}
