<?php

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

final class AISBW_Activator
{
    public static function activate(): void
    {
        self::create_tables();
        self::add_capabilities();
        self::add_default_options();
    }

    private static function create_tables(): void
    {
        global $wpdb;

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';

        $charset_collate = $wpdb->get_charset_collate();
        $topics_table = $wpdb->prefix . 'aisbw_topics';
        $logs_table = $wpdb->prefix . 'aisbw_logs';
        $runs_table = $wpdb->prefix . 'aisbw_runs';

        $sql = "
CREATE TABLE {$topics_table} (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    topic VARCHAR(255) NOT NULL,
    working_title VARCHAR(255) NULL,
    primary_keyword VARCHAR(255) NULL,
    secondary_keywords TEXT NULL,
    audience VARCHAR(255) NULL,
    search_intent TEXT NULL,
    category_id BIGINT UNSIGNED NULL,
    tags TEXT NULL,
    priority INT DEFAULT 50,
    risk_level VARCHAR(50) DEFAULT 'normal',
    publish_mode VARCHAR(50) DEFAULT 'draft_only',
    language VARCHAR(50) DEFAULT 'pl' NULL,
    status VARCHAR(50) DEFAULT 'idea',
    brief LONGTEXT NULL,
    generated_content LONGTEXT NULL,
    quality_score INT NULL,
    quality_result LONGTEXT NULL,
    seo_title VARCHAR(255) NULL,
    seo_description TEXT NULL,
    slug VARCHAR(255) NULL,
    excerpt TEXT NULL,
    wp_post_id BIGINT UNSIGNED NULL,
    notes TEXT NULL,
    last_error TEXT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NOT NULL,
    PRIMARY KEY  (id),
    KEY status (status),
    KEY priority (priority),
    KEY wp_post_id (wp_post_id)
) {$charset_collate};

CREATE TABLE {$logs_table} (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    topic_id BIGINT UNSIGNED NULL,
    level VARCHAR(20) DEFAULT 'info',
    action VARCHAR(100) NOT NULL,
    message TEXT NULL,
    context LONGTEXT NULL,
    created_at DATETIME NOT NULL,
    PRIMARY KEY  (id),
    KEY topic_id (topic_id),
    KEY level (level),
    KEY action (action),
    KEY created_at (created_at)
) {$charset_collate};

CREATE TABLE {$runs_table} (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    topic_id BIGINT UNSIGNED NULL,
    run_type VARCHAR(50) NOT NULL,
    status VARCHAR(50) DEFAULT 'started',
    input_json LONGTEXT NULL,
    output_json LONGTEXT NULL,
    tokens_input INT NULL,
    tokens_output INT NULL,
    cost_estimate DECIMAL(10,4) NULL,
    error_message TEXT NULL,
    created_at DATETIME NOT NULL,
    finished_at DATETIME NULL,
    PRIMARY KEY  (id),
    KEY topic_id (topic_id),
    KEY run_type (run_type),
    KEY status (status),
    KEY created_at (created_at)
) {$charset_collate};
";

        dbDelta($sql);
    }

    private static function add_capabilities(): void
    {
        $role = get_role('administrator');

        if ($role) {
            $role->add_cap('manage_guco_ai_blog_writer');
        }
    }

    private static function add_default_options(): void
    {
        $defaults = [
            'aisbw_default_article_length' => '1200-1600 words',
            'aisbw_default_tone' => 'practical, simple, specific',
            'aisbw_default_language' => 'pl',
            'aisbw_default_category_id' => 0,
            'aisbw_default_author_id' => 0,
            'aisbw_test_mode_enabled' => 0,
            'aisbw_autopublish_enabled' => 0,
            'aisbw_min_quality_score' => 90,
            'aisbw_cron_enabled' => 0,
            'aisbw_max_articles_per_day' => 3,
            'aisbw_max_ai_requests_per_day' => 20,
        ];

        foreach ($defaults as $name => $value) {
            if (false === get_option($name, false)) {
                add_option($name, $value);
            }
        }
    }
}
