<?php
/**
 * Coupon Generator AI Agent.
 *
 * @since   1.1.4
 * @package SAAI
 */

namespace SAAI\AI_Agents\Advanced_Coupons;

use SAAI\Abstracts\Abstract_AI_Agent;
use SAAI\Traits\Singleton_Trait;

defined( 'ABSPATH' ) || exit;

/**
 * Coupon Generator AI Agent class.
 *
 * AI-powered coupon generation for targeted discount campaigns
 * with Advanced Coupons for WooCommerce integration.
 *
 * @since 1.1.4
 */
class Coupon_Generator_AI extends Abstract_AI_Agent {

    use Singleton_Trait;

    /**
     * Holds the class instance object.
     *
     * @since 1.1.4
     * @var Coupon_Generator_AI $instance object
     */
    protected static $instance;

    /**
     * Holds the settings data.
     *
     * @since 1.1.4
     * @var array<string, mixed> Settings data.
     */
    protected $settings;

    /**
     * Advanced Coupons Free plugin file.
     *
     * @since 1.1.4
     * @var string
     */
    const ACFW_PLUGIN = 'advanced-coupons-for-woocommerce-free/advanced-coupons-for-woocommerce.php';

    /**
     * Advanced Coupons Premium plugin file.
     *
     * @since 1.1.4
     * @var string
     */
    const ACFWP_PLUGIN = 'advanced-coupons-for-woocommerce/advanced-coupons-for-woocommerce.php';

    /**
     * Constructor.
     *
     * @since 1.1.4
     *
     * @param array|null $data Optional prompt data to send to AI.
     */
    public function __construct( $data = null ) {
        $this->endpoint         = 'coupon-generator';
        $this->ai_id            = $this->endpoint . '-ai';
        $this->data             = $data;
        $this->default_settings = array(
            'enabled' => $this->maybe_enable_by_default(),
        );

        // Initialize settings.
        $this->settings = $this->get_settings();
    }

    /**
     * Check if Advanced Coupons Free is active.
     *
     * @since 1.1.4
     *
     * @return bool True if Advanced Coupons is active.
     */
    public function is_acfw_active() {
        // First check if ACFW class exists (more reliable).
        if ( class_exists( 'ACFWF' ) ) {
            return true;
        }

        // Fallback to plugin check.
        $this->ensure_plugin_functions_loaded();

        return \is_plugin_active( self::ACFW_PLUGIN );
    }

    /**
     * Check if Advanced Coupons Premium is active.
     *
     * @since 1.1.4
     *
     * @return bool True if Advanced Coupons Premium is active.
     */
    public function is_acfwp_active() {
        // First check if ACFWP class exists (more reliable).
        if ( class_exists( 'ACFWP' ) ) {
            return true;
        }

        // Fallback to plugin check.
        $this->ensure_plugin_functions_loaded();

        return \is_plugin_active( self::ACFWP_PLUGIN );
    }

    /**
     * Ensure WordPress plugin functions are loaded.
     *
     * @since 1.1.4
     * @return void
     */
    private function ensure_plugin_functions_loaded() {
        if ( ! function_exists( '\is_plugin_active' ) ) {
            require_once \ABSPATH . 'wp-admin/includes/plugin.php';
        }
    }

    /**
     * Check if agent is enabled.
     *
     * @since 1.1.4
     *
     * @return bool True if enabled and Advanced Coupons is active.
     */
    public function is_enabled() {
        return parent::is_enabled() && $this->is_acfw_active();
    }

    /**
     * Maybe enable by default.
     *
     * Automatically enable when Advanced Coupons is active.
     *
     * @since 1.1.4
     *
     * @return string 'yes' if AC is active, 'no' otherwise.
     */
    public function maybe_enable_by_default() {
        // Auto-enable if Advanced Coupons is active.
        return $this->is_acfw_active() ? 'yes' : 'no';
    }

    /**
     * Run the AI agent.
     *
     * Register hooks and initialize functionality.
     *
     * @since 1.1.4
     *
     * @return void
     */
    public function run() {
        if ( ! $this->is_enabled() ) {
            return;
        }

        // Coupon generation functionality is handled by REST endpoints.
        // No additional hooks needed at this time.
    }
}
