<?php

/**
 * Costudio_Admin — admin façade + AJAX dispatcher.
 *
 * Thin orchestrator: wires up the menu/assets controllers and delegates
 * every AJAX domain to a dedicated Costudio_Admin_Ajax_* class. Also
 * exposes façade statics (language resolution, agent-prompt defaults,
 * NONCE / throttle constants) consumed by views, AJAX controllers and
 * the pipeline.
 *
 * Delegates:
 *   - Menu / wizard redirect     → Costudio_Admin_Menu
 *   - Assets                     → Costudio_Admin_Assets
 *   - Sites AJAX                 → Costudio_Admin_Sites
 *   - Brand Voice AJAX           → Costudio_Admin_Brand_Voice (optional)
 *   - Planning AJAX              → Costudio_Admin_Ajax_Planning
 *   - Misc AJAX (prompts, etc.)  → Costudio_Admin_Ajax_Misc
 *   - Settings + diagnostics     → Costudio_Admin_Ajax_Settings
 *   - Production lifecycle       → Costudio_Admin_Ajax_Production
 *   - Content Hub + Dashboard    → Costudio_Admin_Ajax_Tables
 *                                  (render helpers live in Costudio_Admin_Tables)
 *
 * @package Contentosapp_Studio
 * @since   3.0.0
 */

defined('ABSPATH') || exit;

class Costudio_Admin
{

    /**
     * Singleton instance.
     */
    private static ?self $instance = null;

    /** AJAX nonce action shared across all Contentosapp Studio requests. */
    const NONCE = 'costudio_nonce';

    /** Minimum interval between diagnostic API calls (seconds). */
    const DIAG_THROTTLE_SECONDS = 5;

    /**
     * Private constructor to enforce singleton.
     */
    private function __construct() {}

    /**
     * Get the singleton instance.
     */
    public static function get_instance(): self
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Get the default language for UI from settings.
     */
    public static function get_default_language(): string
    {
        $cts = costudio_settings();
        return $cts ? (string) $cts->get('default_language', 'en_US') : 'en_US';
    }

    /**
     * Get the default language for Content (AI Output) from settings.
     */
    public static function get_default_content_language(): string
    {
        $cts = costudio_settings();
        $lang = $cts ? (string) $cts->get('default_content_language', 'en_US') : 'en_US';
        return self::is_supported_content_language($lang) ? $lang : 'en_US';
    }

    /**
     * Content languages supported by production prompts and PHP renderers.
     *
     * @return array<string,string> Locale code => reader-facing language name.
     */
    public static function get_supported_content_languages(): array
    {
        return [
            'pt_BR' => 'Português do Brasil',
            'en_US' => 'English (US)',
            'es_ES' => 'Español (España)',
        ];
    }

    public static function is_supported_content_language(string $lang): bool
    {
        return isset(self::get_supported_content_languages()[$lang]);
    }

    public static function get_content_language_name(string $lang): string
    {
        return self::get_supported_content_languages()[$lang] ?? '';
    }

    /**
     * Default prompt text for an agent.
     * In the new Secure Architecture, we no longer ship plaintext prompts.
     * This method now only returns what is in the DB. If empty, the Pipeline
     * will delegate execution to the ContentOS Cloud API.
     */
    public static function get_agent_default_prompt_text(int $agent, ?string $lang = null): string
    {
        return (string) costudio_get_option(sprintf('costudio_agent_%02d', $agent), '');
    }

    /**
     * Retorna true se o prompt salvo corresponde ao padrão do agente.
     * No modelo novo, "Default" é o que o Seeder baixou da API (mesmo que seja um teaser).
     */
    public static function prompt_is_default_for_agent(string $stored, int $agent): bool
    {
        if (empty($stored)) {
            return true;
        }

        // We could compare with a "costudio_agent_default_XX" option if we had one,
        // but for now, we'll rely on the UI/Ajax logic to determine if it's customized.
        return false; 
    }

	// ── Bootstrap ─────────────────────────────────────────────────────────────

    /**
     * Called by Costudio_Standalone_Bootstrap — registers all hooks.
     */
    public function register_hooks(): void
    {
        // Menus + wizard redirect
        Costudio_Admin_Menu::get_instance()->register_hooks();

        // Assets
        Costudio_Admin_Assets::get_instance()->register_hooks();

        // Delegate sites AJAX
        Costudio_Admin_Sites::get_instance()->register_hooks();

        // Delegate Brand Voice AJAX
        if ( class_exists( 'Costudio_Admin_Brand_Voice' ) ) {
            Costudio_Admin_Brand_Voice::register_hooks();
        }

        // Delegate planning AJAX
        Costudio_Admin_Ajax_Planning::get_instance()->register_hooks();

        // Delegate misc AJAX (prompts, local site, toggles, welcome)
        Costudio_Admin_Ajax_Misc::get_instance()->register_hooks();

        // Delegate settings + diagnostics AJAX (API keys, models, connection tests)
        Costudio_Admin_Ajax_Settings::get_instance()->register_hooks();

        // Delegate production lifecycle AJAX (fetch/start/poll/publish/export/sync)
        Costudio_Admin_Ajax_Production::get_instance()->register_hooks();

        // Delegate Content Hub + Dashboard + bulk action AJAX (render/KPIs)
        Costudio_Admin_Ajax_Tables::get_instance()->register_hooks();
    }

    /**
     * Back-compat shim: delegates to Costudio_Admin_Tables::get_content_hub_kpis().
     *
     * Kept because ContentOS Core's license view
     * (contentos-core/admin/views/license.php) calls this static method
     * defensively via class_exists + is_callable. Removing it would break
     * the license page when both plugins are active.
     *
     * @return array<string, string|int>
     */
    public static function get_content_hub_kpis(): array
    {
        return Costudio_Admin_Tables::get_content_hub_kpis();
    }

}
