<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Automation Manager
 *
 * Handles registration and execution of automation workflows, triggers, and actions.
 *
 * @package    Kitsune_AI_Platform
 * @subpackage Kitsune_AI_Platform/includes/automation
 */

class Kitsune_AI_Platform_Automation_Manager {

    /**
     * The unique identifier of this plugin
     */
    private $plugin_name;

    /**
     * The current version of the plugin
     */
    private $version;

    /**
     * Registered Triggers
     */
    private $triggers;

    /**
     * Registered Actions
     */
    private $actions;

    /**
     * Logger instance
     */
    private $logger;

    /**
     * Singleton Instance
     */
    private static $instance = null;

    /**
     * Get instance
     */
    public static function get_instance() {
        return self::$instance;
    }

    /**
     * Initialize the class and set its properties.
     *
     * @param string $plugin_name The name of the plugin.
     * @param string $version     The version of the plugin.
     */
    public function __construct($plugin_name, $version) {
        self::$instance = $this;
        $this->plugin_name = $plugin_name;
        $this->version = $version;

        $this->load_dependencies();
        $this->init_components();
    }

    /**
     * Load automation dependencies
     */
    private function load_dependencies() {
        // Dependencies are now loaded via Autoloader
    }

    /**
     * Initialize triggers, actions, and logger
     */
    private function init_components() {
        $this->logger   = Kitsune_AI_Platform_Automation_Logger::get_instance();
        $this->triggers = new Kitsune_AI_Platform_Automation_Triggers();
        $this->actions  = new Kitsune_AI_Platform_Automation_Actions();

        // Hook into workflow execution to add logging
        add_action('kitsune_ai_platform_execute_workflow', array($this, 'before_workflow_execution'), 5, 3);
        add_action('kitsune_ai_platform_execute_workflow', array($this, 'after_workflow_execution'), 999, 3);
    }

    /**
     * Before workflow execution - start trace and log
     */
    public function before_workflow_execution($workflow_id, $config, $context) {
        // Start a new trace for this workflow execution
        Kitsune_AI_Platform_Automation_Logger::start_trace();

        $workflow = get_post($workflow_id);
        $workflow_name = $workflow ? $workflow->post_title : __('Unknown', 'kitsune-ai-platform');

        $this->logger->log_workflow(
            $workflow_id,
            Kitsune_AI_Platform_Automation_Logger::LEVEL_INFO,
            /* translators: %s: Workflow name. */
            sprintf(__('Workflow "%s" started execution', 'kitsune-ai-platform'), $workflow_name),
            array(
                'trigger_type' => $config['trigger']['type'] ?? 'unknown',
                'context_keys' => array_keys($context)
            )
        );
    }

    /**
     * After workflow execution - log completion and end trace
     */
    public function after_workflow_execution($workflow_id, $config, $context) {
        $workflow = get_post($workflow_id);
        $workflow_name = $workflow ? $workflow->post_title : __('Unknown', 'kitsune-ai-platform');

        $this->logger->log_workflow(
            $workflow_id,
            Kitsune_AI_Platform_Automation_Logger::LEVEL_INFO,
            /* translators: %s: Workflow name. */
            sprintf(__('Workflow "%s" completed execution', 'kitsune-ai-platform'), $workflow_name),
            array('trace_id' => Kitsune_AI_Platform_Automation_Logger::get_trace_id())
        );

        // End the trace
        Kitsune_AI_Platform_Automation_Logger::end_trace();
    }

    /**
     * Get available components for the UI
     * 
     * @return array
     */
    public function get_components() {
        return array(
            'triggers' => $this->triggers->get_registered_triggers(),
            'actions'  => $this->actions->get_registered_actions()
        );
    }
}
