<?php

if ( !defined( 'ABSPATH' ) ) {
    exit;
}
final class AISBW_Plugin {
    private static ?AISBW_Plugin $instance = null;

    public static function instance() : AISBW_Plugin {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public static function activate() : void {
        self::load_dependencies();
        AISBW_Activator::activate();
    }

    public static function deactivate() : void {
        self::load_dependencies();
        AISBW_Deactivator::deactivate();
    }

    public function init() : void {
        self::load_dependencies();
        add_action( 'init', [$this, 'load_textdomain'] );
        add_filter(
            'http_request_args',
            static function ( array $args, string $url ) : array {
                if ( str_contains( $url, 'api.openai.com' ) || str_contains( $url, 'generativelanguage.googleapis.com' ) || str_contains( $url, 'oaidalleapiprodscus' ) ) {
                    $args['timeout'] = max( (int) ($args['timeout'] ?? 0), 300 );
                    $args['connect_timeout'] = max( (int) ($args['connect_timeout'] ?? 0), 30 );
                }
                return $args;
            },
            99999,
            2
        );
        add_filter(
            'http_request_timeout',
            static function ( $timeout, string $url ) {
                if ( str_contains( $url, 'api.openai.com' ) || str_contains( $url, 'generativelanguage.googleapis.com' ) || str_contains( $url, 'oaidalleapiprodscus' ) ) {
                    return max( (int) $timeout, 300 );
                }
                return $timeout;
            },
            99999,
            2
        );
        AISBW_Settings::instance()->init();
        if ( class_exists( 'AISBW_Cron' ) ) {
            AISBW_Cron::instance()->init();
        }
        if ( is_admin() ) {
            AISBW_Admin_Menu::instance()->init();
        }
    }

    public function load_textdomain() : void {
        load_plugin_textdomain( 'guco-ai-blog-writer', false, dirname( plugin_basename( AISBW_PLUGIN_FILE ) ) . '/languages' );
    }

    private static function load_dependencies() : void {
        static $loaded = false;
        if ( $loaded ) {
            return;
        }
        $files = [
            'class-activator.php',
            'class-deactivator.php',
            'class-settings.php',
            'class-admin-menu.php',
            'class-topic-repository.php',
            'class-run-repository.php',
            'class-logger.php',
            'class-ai-client.php',
            'class-content-generator.php',
            'class-quality-checker.php',
            'class-post-creator.php',
            'class-seo-adapter.php'
        ];
        foreach ( $files as $file ) {
            require_once AISBW_PLUGIN_DIR . 'includes/' . $file;
        }
        if ( function_exists( 'aisbw_fs' ) && aisbw_fs()->can_use_premium_code__premium_only() ) {
            $cron_file = AISBW_PLUGIN_DIR . 'includes/class-cron-premium.php';
            if ( file_exists( $cron_file ) ) {
                require_once $cron_file;
            }
            $multilingual_file = AISBW_PLUGIN_DIR . 'includes/class-multilingual-premium.php';
            if ( file_exists( $multilingual_file ) ) {
                require_once $multilingual_file;
            }
            $autopublish_file = AISBW_PLUGIN_DIR . 'includes/class-autopublish-premium.php';
            if ( file_exists( $autopublish_file ) ) {
                require_once $autopublish_file;
            }
            $featured_image_file = AISBW_PLUGIN_DIR . 'includes/class-featured-image-premium.php';
            if ( file_exists( $featured_image_file ) ) {
                require_once $featured_image_file;
            }
        }
        $loaded = true;
    }

    private function __construct() {
    }

}
