<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /migrations/022_abandoned_carts_sequence.php
// Description: Add nudge_1_sent_at, nudge_2_sent_at, nudge_3_sent_at columns to wp_amai_abandoned_carts - NUCLEAR+++
// Last Edited: 2026-04-22
// ==================================================
namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

final class Migration_022_AbandonedCartsSequence
{
    public function up(): void
    {
        global $wpdb;
        $table = $wpdb->prefix . 'amai_abandoned_carts';

        $existing = $wpdb->get_results(
            "SHOW COLUMNS FROM `{$table}`",
            ARRAY_A
        );
        $cols = array_column($existing, 'Field');

        if (!in_array('nudge_1_sent_at', $cols, true)) {
            $wpdb->query(
                "ALTER TABLE `{$table}` ADD COLUMN `nudge_1_sent_at` DATETIME NULL DEFAULT NULL AFTER `sent_at`"
            );
        }

        if (!in_array('nudge_2_sent_at', $cols, true)) {
            $wpdb->query(
                "ALTER TABLE `{$table}` ADD COLUMN `nudge_2_sent_at` DATETIME NULL DEFAULT NULL AFTER `nudge_1_sent_at`"
            );
        }

        if (!in_array('nudge_3_sent_at', $cols, true)) {
            $wpdb->query(
                "ALTER TABLE `{$table}` ADD COLUMN `nudge_3_sent_at` DATETIME NULL DEFAULT NULL AFTER `nudge_2_sent_at`"
            );
        }
    }
}