<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/025_email_contacts.php
// Description: Create wp_amai_email_contacts table for email provider contact/list management - NUCLEAR+++
// Last Edited: 2026-04-27
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

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

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

        $sql = "CREATE TABLE IF NOT EXISTS `{$table}` (" .
            "`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT," .
            "`email` varchar(254) NOT NULL," .
            "`first_name` varchar(100) DEFAULT NULL," .
            "`last_name` varchar(100) DEFAULT NULL," .
            "`status` enum('subscribed','unsubscribed','bounced','complained') NOT NULL DEFAULT 'subscribed'," .
            "`source` varchar(64) NOT NULL DEFAULT 'wp_user'," .
            "`provider_id` varchar(64) DEFAULT NULL," .
            "`list_ids` text DEFAULT NULL," .
            "`unsubscribed_at` datetime DEFAULT NULL," .
            "`bounced_at` datetime DEFAULT NULL," .
            "`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_email` (`email`)," .
            "KEY `idx_status` (`status`)," .
            "KEY `idx_source` (`source`)" .
            ") {$charset};";

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