<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/024_blog_schedule.php
// Description: Create wp_amai_blog_schedule table for blog post nudge and auto-draft scheduling - NUCLEAR+++
// Last Edited: 2026-04-25
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

class Migration_024_BlogSchedule
{
    public function up(): void
    {
        global $wpdb;

        $table   = $wpdb->prefix . 'amai_blog_schedule';
        $charset = $wpdb->get_charset_collate();

        $sql = "CREATE TABLE IF NOT EXISTS `{$table}` (
            `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
            `slot_date` date NOT NULL COMMENT 'Scheduled publish date (Mon/Wed/Fri)',
            `slot_time` time NOT NULL DEFAULT '09:00:00' COMMENT 'Scheduled nudge time',
            `status` enum('pending','nudged','acted','drafted','published','skipped') NOT NULL DEFAULT 'pending',
            `titles_json` longtext DEFAULT NULL COMMENT 'JSON array of 5 AI-generated title options',
            `nudge_sent_at` datetime DEFAULT NULL,
            `auto_draft_at` datetime DEFAULT NULL,
            `draft_post_id` bigint(20) UNSIGNED DEFAULT NULL,
            `published_post_id` bigint(20) UNSIGNED DEFAULT NULL,
            `nudge_token` varchar(64) DEFAULT NULL COMMENT 'Token for email title-click links',
            `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (`id`),
            UNIQUE KEY `uq_slot_date` (`slot_date`)
        ) {$charset};";

        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta($sql);
    }
}