<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/045_clusters.php
// Description: Create wp_amai_clusters and wp_amai_cluster_posts tables - NUCLEAR+++
// Last Edited: 2026-05-18
// ==================================================
namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

class Migration_045_Clusters
{
    public function up(): void
    {
        global $wpdb;
        $charset = $wpdb->get_charset_collate();

        $wpdb->query(
            "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}amai_clusters` (
                `id`             INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `topic`          VARCHAR(500) NOT NULL DEFAULT '',
                `pillar_post_id` BIGINT UNSIGNED NOT NULL DEFAULT 0,
                `status`         ENUM('pending','active','complete') NOT NULL DEFAULT 'pending',
                `created_at`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
                `updated_at`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                PRIMARY KEY (`id`),
                KEY `idx_status` (`status`)
            ) {$charset}"
        );

        $wpdb->query(
            "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}amai_cluster_posts` (
                `id`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `cluster_id`  INT UNSIGNED NOT NULL,
                `post_id`     BIGINT UNSIGNED NOT NULL DEFAULT 0,
                `post_role`   ENUM('pillar','supporting') NOT NULL DEFAULT 'supporting',
                `slot_date`   DATE NOT NULL,
                `status`      ENUM('scheduled','published','failed') NOT NULL DEFAULT 'scheduled',
                `created_at`  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
                `updated_at`  DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
                PRIMARY KEY (`id`),
                KEY `idx_cluster_id` (`cluster_id`),
                KEY `idx_slot_date` (`slot_date`)
            ) {$charset}"
        );
    }
}