<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /migrations/020_abandoned_carts.php
// Description: Creates amai_abandoned_carts table for WooCommerce abandoned cart recovery - NUCLEAR+++
// Last Edited: 2026-04-21
// ==================================================
namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

final class Migration_020_AbandonedCarts
{
    public const VERSION     = '020';
    public const DESCRIPTION = 'Create amai_abandoned_carts table';

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

        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}amai_abandoned_carts` (
            `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `email`         VARCHAR(191)    NOT NULL DEFAULT '',
            `first_name`    VARCHAR(100)    NOT NULL DEFAULT '',
            `cart_contents` LONGTEXT        NOT NULL,
            `cart_total`    VARCHAR(32)     NOT NULL DEFAULT '',
            `checkout_url`  TEXT            NOT NULL,
            `status`        ENUM('pending','sent','recovered','cancelled') NOT NULL DEFAULT 'pending',
            `sent_at`       DATETIME        NULL DEFAULT NULL,
            `opened_at`     DATETIME        NULL DEFAULT NULL,
            `open_count`    INT UNSIGNED    NOT NULL DEFAULT 0,
            `created_at`    DATETIME        NOT NULL,
            PRIMARY KEY (`id`),
            KEY `status_created` (`status`, `created_at`),
            KEY `email` (`email`)
        ) {$charset};";

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