<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/028_abandoned_carts_unsub_token.php
// Description: Adds unsubscribe_token column to wp_amai_abandoned_carts - NUCLEAR+++
// Last Edited: 2026-05-09
// ==================================================

namespace AutoMarketerAI\Migrations;

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

        $table = $wpdb->prefix . 'amai_abandoned_carts';

        // Idempotent -- skip if column already exists
        $col = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = 'unsubscribe_token'",
                DB_NAME,
                $table
            )
        );

        if ($col !== null) {
            return;
        }

        $wpdb->query("ALTER TABLE `{$table}` ADD COLUMN `unsubscribe_token` VARCHAR(64) NULL DEFAULT NULL");
    }
}