<?php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

use Carbon_Fields\Container;
use Carbon_Fields\Field;

// Hook into WordPress with prefixed function names
add_action( 'after_setup_theme', 'copianto_load_carbon_fields' );
add_action( 'carbon_fields_register_fields', 'copianto_create_options_page' );

// Enqueue styles/scripts properly instead of echoing
add_action( 'wp_enqueue_scripts', 'copianto_enqueue_widget_assets' );

// Render markup hooks
add_action( 'wp_head', 'copianto_render_widget_head_markup' );
add_action( 'wp_footer', 'copianto_render_widget_footer_markup' );

/**
 * Boot Carbon Fields
 */
function copianto_load_carbon_fields() {
    \Carbon_Fields\Carbon_Fields::boot();
}

/**
 * Create the options page in WordPress admin
 */
function copianto_create_options_page() {
    Container::make( 'theme_options', __( 'Copianto Plugin', 'copianto' ) )
        ->set_icon( 'dashicons-plugins-checked' )
        ->add_fields(
            array(
                Field::make( 'checkbox', 'copianto_plugin_active', __( 'Active', 'copianto' ) ),

                Field::make( 'radio', 'copianto_plugin_option', __( 'Copianto Option', 'copianto' ) )
                    ->add_options( array(
                        'chat'   => __( 'Copianto Chat', 'copianto' ),
                        // 'search' => __( 'Copianto Search', 'copianto' ),
                    )
                ),

                Field::make( 'text', 'copianto_public_key', __( 'Copianto Public Key', 'copianto' ) )
                    ->set_attribute( 'placeholder', '********************' )
                    ->set_help_text( __( 'Check your Copianto account dashboard for this key!', 'copianto' ) ),
            )
        );
}

/**
 * Enqueue widget styles and scripts
 */
function copianto_enqueue_widget_assets() {
    $plugin_status = carbon_get_theme_option( 'copianto_plugin_active' );
    $copianto_public_key = carbon_get_theme_option( 'copianto_public_key' );

    if ( $plugin_status && ! empty( $copianto_public_key ) ) {
        // Enqueue CSS
        // wp_enqueue_style(
        //     'copianto-widget-css',
        //     'https://assets.copianto.ai/widget/v1/copianto-widget.css',
        //     array(),
        //     null
        // );

        // Enqueue JS in footer
        // wp_enqueue_script(
        //     'copianto-widget-js',
        //     'https://assets.copianto.ai/widget/v1/copianto-widget.js',
        //     array(),
        //     null,
        //     true
        // );
        wp_enqueue_script(
            'copianto-widget-js',
            'https://unpkg.com/@copianto/chat-widget@latest',
            array(),
            null,
            true
        );
    }
}

/**
 * Output any <head> markup if needed (now mostly handled by enqueue)
 */
function copianto_render_widget_head_markup() {
    // No inline CSS/JS here since assets are enqueued properly
}

/**
 * Render widget HTML markup in footer
 */
function copianto_render_widget_footer_markup() {
    $plugin_status = carbon_get_theme_option( 'copianto_plugin_active' );
    $copianto_public_key = carbon_get_theme_option( 'copianto_public_key' );
    $copianto_plugin_option = carbon_get_theme_option( 'copianto_plugin_option' );

    if ( $plugin_status && ! empty( $copianto_public_key ) ) {
        if ( $copianto_plugin_option === 'chat' ) {
            echo '<div id="copianto-chat" data-public-key="' . esc_attr( $copianto_public_key ) . '"></div>';
        } else {
            echo '<div id="copianto-search" data-public-key="' . esc_attr( $copianto_public_key ) . '"></div>';
        }
    }
}
