<?php
/**
 * Plugin Name:       Contentosapp Studio
 * Plugin URI:        https://contentosapp.com/contentosapp-studio
 * Description:       Generate complete SEO articles with 7 specialized AI agents and publish directly to your WordPress site. Free to use with Google Gemini.
 * Version:           1.6.1
 * Author:            ContentOS
 * Author URI:        https://contentosapp.com
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       contentosapp-studio
 * Domain Path:       /languages
 * Requires at least: 6.0
 * Requires PHP:      7.4
 */

defined( 'ABSPATH' ) || exit;

// PHP version guard
if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
	add_action( 'admin_notices', static function () {
		echo '<div class="notice notice-error"><p>';
		echo esc_html__( 'Contentosapp Studio requires PHP 7.4 or higher. Please update your PHP version.', 'contentosapp-studio' );
		echo '</p></div>';
	} );
	return;
}

// WordPress version guard
if ( version_compare( get_bloginfo( 'version' ), '6.0', '<' ) ) {
	add_action( 'admin_notices', static function () {
		echo '<div class="notice notice-error"><p>';
		echo esc_html__( 'Contentosapp Studio requires WordPress 6.0 or higher. Please update WordPress.', 'contentosapp-studio' );
		echo '</p></div>';
	} );
	return;
}

// Constants (Must be defined before Core detection to avoid "Undefined constant" errors)
if ( ! defined( 'COSTUDIO_VERSION' ) ) {
	define( 'COSTUDIO_VERSION', '1.6.1' );
}
if ( ! defined( 'COSTUDIO_DIR' ) ) {
	define( 'COSTUDIO_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'COSTUDIO_URL' ) ) {
	define( 'COSTUDIO_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'COSTUDIO_PLUGIN_BASENAME' ) ) {
	define( 'COSTUDIO_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}

if ( ! defined( 'COSTUDIO_STORE_URL' ) ) {
	define( 'COSTUDIO_STORE_URL', 'https://contentosapp.com' );
}
if ( ! defined( 'COSTUDIO_UPGRADE_URL' ) ) {
	define( 'COSTUDIO_UPGRADE_URL', 'https://contentosapp.com/?utm_source=plugin&utm_medium=standalone&utm_campaign=upgrade#pricing' );
}
if ( ! defined( 'COSTUDIO_FOS_PRO_URL' ) ) {
	define( 'COSTUDIO_FOS_PRO_URL', 'https://contentosapp.com/?utm_source=plugin&utm_medium=standalone#pricing' );
}

if ( ! defined( 'COSTUDIO_API_URL' ) ) {
	define( 'COSTUDIO_API_URL', 'https://api.contentosapp.com' );
}
if ( ! defined( 'COSTUDIO_REMOTE_AJAX_PATH' ) ) {
	define( 'COSTUDIO_REMOTE_AJAX_PATH', '/' . 'wp-admin' . '/' . 'admin-ajax.php' );
}

// Contentosapp Studio text domain: register plugin_locale before plugins_loaded:5 (ContentOS JIT / early __()).
require_once COSTUDIO_DIR . 'includes/costudio-i18n-locale.php';
require_once COSTUDIO_DIR . 'includes/costudio-options.php';
require_once COSTUDIO_DIR . 'includes/costudio-logging.php';
add_action( 'plugins_loaded', 'costudio_register_ui_locale_filter', 1 );

// Bootstrap early so we have the autoloader available even during migration.
require_once COSTUDIO_DIR . 'includes/class-costudio-standalone-bootstrap.php';
spl_autoload_register( function ( string $class ) {
    if ( class_exists( $class, false ) ) {
        return;
    }
    $class_map = [
        'Costudio_Standalone_Crypto'   => 'includes/class-costudio-standalone-crypto.php',
        'Costudio_Standalone_Settings' => 'includes/class-costudio-standalone-settings.php',
        'Costudio_Standalone_Sites'    => 'includes/class-costudio-standalone-sites.php',
        'Costudio_Standalone_License'  => 'includes/class-costudio-standalone-license.php',
        'Costudio_Standalone_Migrator' => 'includes/class-costudio-standalone-migrator.php',
        'Costudio_Search_Engine_Gemini' => 'includes/class-costudio-search-engine-gemini.php',
        'Costudio_Content_Word_Count'  => 'includes/class-costudio-content-word-count.php',
        'Costudio_Production_Queue'    => 'includes/class-costudio-production-queue.php',
        'Costudio_Publication_Ledger'  => 'includes/class-costudio-publication-ledger.php',
        'Costudio_Publication_Recovery'=> 'includes/class-costudio-publication-recovery.php',
		'Costudio_Publication_Jobs'     => 'includes/class-costudio-publication-jobs.php',
		'Costudio_Publication_Worker'   => 'includes/class-costudio-publication-worker.php',
		'Costudio_Publication_Jobs_CLI' => 'includes/class-costudio-publication-jobs-cli.php',
		'Costudio_Remote_Jobs'         => 'includes/class-costudio-remote-jobs.php',
		'Costudio_BYOK_Jobs'           => 'includes/class-costudio-byok-jobs.php',
		'Costudio_BYOK_Jobs_CLI'       => 'includes/class-costudio-byok-jobs-cli.php',
		'Costudio_BYOK_Job_Codec'      => 'includes/class-costudio-byok-job-codec.php',
		'Costudio_BYOK_Gateway'        => 'includes/class-costudio-byok-gateway.php',
		'Costudio_BYOK_Provider_Executor' => 'includes/class-costudio-byok-provider-executor.php',
		'Costudio_BYOK_Worker'         => 'includes/class-costudio-byok-worker.php',
        'Costudio_ContentOS_Adapter'   => 'includes/adapters/class-costudio-contentos-adapter.php',
        'Costudio_Pipeline_Input_Mapper' => 'includes/pipeline/class-costudio-pipeline-input-mapper.php',
        'Costudio_Pipeline_Links'      => 'includes/pipeline/class-costudio-pipeline-links.php',
        'Costudio_Link_Reader'         => 'includes/pipeline/class-costudio-link-reader.php',
        'Costudio_Pipeline_Summaries'  => 'includes/pipeline/class-costudio-pipeline-summaries.php',
        'Costudio_Pipeline_Image_Markers' => 'includes/pipeline/class-costudio-pipeline-image-markers.php',
        'Costudio_Publisher_Text_Extractor' => 'includes/publisher/class-costudio-publisher-text-extractor.php',
        'Costudio_Publisher_Content_Normalizer' => 'includes/publisher/class-costudio-publisher-content-normalizer.php',
        'Costudio_Publisher_Image_Prompts' => 'includes/publisher/class-costudio-publisher-image-prompts.php',
        'Costudio_Publisher_Schema'    => 'includes/publisher/class-costudio-publisher-schema.php',
        'Costudio_Publisher_Markdown_Renderer' => 'includes/publisher/class-costudio-publisher-markdown-renderer.php',
        'Costudio_Brand_Voice'         => 'includes/class-costudio-brand-voice.php',
        'Costudio_Editorial_Flags'     => 'includes/class-costudio-editorial-flags.php',
        'Costudio_Url'                 => 'includes/class-costudio-url.php',
        'Costudio_Admin_Brand_Voice'   => 'admin/class-costudio-admin-brand-voice.php',
        'Costudio_Admin_Model_Discovery' => 'admin/helpers/class-costudio-admin-model-discovery.php',
        'Costudio_Admin_Production_Lifecycle' => 'admin/helpers/class-costudio-admin-production-lifecycle.php',
        'Costudio_Admin_Production_Validator' => 'admin/helpers/class-costudio-admin-production-validator.php',
        'Costudio_Admin_Production_Exporter' => 'admin/helpers/class-costudio-admin-production-exporter.php',
        'Costudio_Admin_Remote_Post_Sync' => 'admin/helpers/class-costudio-admin-remote-post-sync.php',
        'Costudio_Article_Types'       => 'includes/class-costudio-article-types.php',
        'Costudio_Update_Sectioner'    => 'includes/update/class-costudio-update-sectioner.php',
    ];

    if ( isset( $class_map[ $class ] ) ) {
        $file = COSTUDIO_DIR . $class_map[ $class ];
        if ( file_exists( $file ) ) {
            require_once $file;
        }
    }
} );

// If ContentOS Core is active, enter Migration Mode.
if ( defined( 'CONTENTOS_VERSION' ) ) {
	Costudio_Standalone_Bootstrap::register_migration_mode_hooks();
	return;
}

register_activation_hook( __FILE__,   array( 'Costudio_Standalone_Bootstrap', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'Costudio_Standalone_Bootstrap', 'deactivate' ) );

add_action( 'plugins_loaded', array( 'Costudio_Standalone_Bootstrap', 'init' ), 10 );
