<?php

/**
 * Plugin Name: Thia Aide AI Content Creator
 * Description: Thia Aide AI Content Creator & Editor AI-powered content generation.
 * Version: 1.0
 * Author: Thia Aide
 * Author URI: https://www.thiaide.com
 * License:     GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain: thia-aide-content-creator
 */

if (!defined('ABSPATH')) {
    exit;
}

define('THIAIDE_CREATOR_VERSION', '1.0');
define('THIAIDE_CREATOR_DIR', plugin_dir_path(__FILE__));
define('THIAIDE_CREATOR_URL', plugin_dir_url(__FILE__));

require_once THIAIDE_CREATOR_DIR . 'includes/class-api-handler.php';
require_once THIAIDE_CREATOR_DIR . 'includes/class-admin.php';
require_once THIAIDE_CREATOR_DIR . 'includes/class-ajax-handler.php';

class ThiaAideContentCreator {
    
    private static $instance = null;
    
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    private function __construct() {
        register_activation_hook(__FILE__, array($this, 'activate'));
        register_deactivation_hook(__FILE__, array($this, 'deactivate'));
        
        if (is_admin()) {
            new ThiaAdmin();
        }
        
        new ThiaAjaxHandler();
    }
    
    public function activate() {
        $this->create_tables();
        add_option('thia_ai_site_style', 'professional');
        add_option('thia_ai_content_length', 'medium');
    }
    
    public function deactivate() {
        // Clean up if needed
    }
    
    private function create_tables() {
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();

        $table_api_keys = $wpdb->prefix . 'thiaide_content_api_keys';
        $sql_api_keys = "CREATE TABLE IF NOT EXISTS `$table_api_keys` (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            api_key VARCHAR(255) NOT NULL,
            created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        ) $charset_collate;";

        $table_settings = $wpdb->prefix . 'thiaide_content_settings';
        $sql_settings = "CREATE TABLE IF NOT EXISTS `$table_settings` (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            language VARCHAR(50) NOT NULL DEFAULT 'bulgarian',
            updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
        ) $charset_collate;";

        $table_stats = $wpdb->prefix . 'thiaide_content_api_stats';
        $sql_stats = "CREATE TABLE IF NOT EXISTS `$table_stats` (
            id bigint(20) NOT NULL AUTO_INCREMENT,
            date date NOT NULL,
            api_calls int(11) DEFAULT 0,
            total_tokens int(11) DEFAULT 0,
            successful_calls int(11) DEFAULT 0,
            failed_calls int(11) DEFAULT 0,
            created datetime DEFAULT CURRENT_TIMESTAMP,
            updated datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY date_unique (date)
        ) $charset_collate;";
        
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
        dbDelta($sql_api_keys);
        dbDelta($sql_settings);
        dbDelta($sql_stats);

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared,             PluginCheck.Security.DirectDB.UnescapedDBParameter
         $language_exists = $wpdb->get_var("SELECT id FROM `$table_settings` LIMIT 1");
        
        if (!$language_exists) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->insert($table_settings, array('language' => ''));
        }
    }
}

ThiaAideContentCreator::get_instance();