<?php

/**
 * AI Magic Context Manager
 *
 * Manages global context (base prompt / company profile) for all AI generations
 *
 * @link       https://insya.com/ai-magic
 * @since      2.1.2
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/includes
 */

/**
 * Context Manager Class
 *
 * Provides centralized access to base prompt and applies it to all AI prompts
 *
 * @package    Ai_Magic
 * @subpackage Ai_Magic/includes
 * @author     AI Magic <info@insya.com>
 */
class AI_Magic_Context
{
    /**
     * Option name for storing base prompt
     */
    const OPTION_KEY = 'ai_magic_base_prompt';

    /**
     * Get the base prompt from database
     *
     * @since    2.1.2
     * @return   string    Base prompt content or empty string
     */
    public static function get_base_prompt()
    {
        return get_option(self::OPTION_KEY, '');
    }

    /**
     * Check if base prompt is configured and not empty
     *
     * @since    2.1.2
     * @return   bool    True if base prompt exists
     */
    public static function has_base_prompt()
    {
        $base_prompt = self::get_base_prompt();
        return !empty(trim($base_prompt));
    }

    /**
     * Apply base prompt context to a given prompt
     *
     * Prepends the base prompt as context before the actual prompt
     *
     * @since    2.1.2
     * @param    string    $prompt    The original prompt
     * @return   string               Prompt with context applied
     */
    public static function apply_context($prompt)
    {
        if (!self::has_base_prompt()) {
            return $prompt;
        }

        $base_prompt = self::get_base_prompt();
        
        // Create contextual wrapper
        $context_wrapper = "=== COMPANY CONTEXT / GLOBAL INSTRUCTIONS ===\n";
        $context_wrapper .= $base_prompt;
        $context_wrapper .= "\n=== END CONTEXT ===\n\n";
        $context_wrapper .= "=== USER REQUEST ===\n";
        $context_wrapper .= $prompt;

        return $context_wrapper;
    }

    /**
     * Save base prompt to database
     *
     * @since    2.1.2
     * @param    string    $content    Base prompt content
     * @return   bool                  Success status
     */
    public static function save_base_prompt($content)
    {
        return update_option(self::OPTION_KEY, sanitize_textarea_field($content));
    }

    /**
     * Get truncated preview of base prompt for UI display
     *
     * @since    2.1.2
     * @param    int       $length    Maximum length
     * @return   string               Truncated preview or empty string
     */
    public static function get_preview($length = 100)
    {
        $base_prompt = self::get_base_prompt();
        
        if (empty($base_prompt)) {
            return '';
        }

        if (strlen($base_prompt) <= $length) {
            return $base_prompt;
        }

        return substr($base_prompt, 0, $length) . '...';
    }

    /**
     * Get base prompt data for JavaScript localization
     *
     * @since    2.1.2
     * @return   array    Data array for wp_localize_script
     */
    public static function get_js_data()
    {
        return array(
            'hasBasePrompt' => self::has_base_prompt(),
            'preview' => self::get_preview(150),
            'fullContent' => self::get_base_prompt(),
            'characterCount' => strlen(self::get_base_prompt())
        );
    }
}
