<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/043_ga4_connection.php
// Description: Migration 043 -- create wp_amai_ga4_connection table - NUCLEAR+++
// Last Edited: 2026-05-18
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

final class Migration_043_Ga4Connection
{
    public const VERSION     = '043';
    public const DESCRIPTION = 'Create amai_ga4_connection table';

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

        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}amai_ga4_connection` (
            `id`                 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
            `tenant_id`          VARCHAR(64)  NOT NULL DEFAULT 'default',
            `google_account_id`  VARCHAR(128) NOT NULL DEFAULT '',
            `property_id`        VARCHAR(64)  NOT NULL DEFAULT '',
            `property_name`      VARCHAR(255) NOT NULL DEFAULT '',
            `access_token_enc`   TEXT         NOT NULL,
            `refresh_token_enc`  TEXT         NOT NULL,
            `token_expires_at`   DATETIME     NOT NULL,
            `connected_at`       DATETIME     NOT NULL,
            `last_verified_at`   DATETIME     NOT NULL,
            `last_synced_at`     DATETIME     NULL DEFAULT NULL,
            `status`             ENUM('active','expired','error') NOT NULL DEFAULT 'active',
            PRIMARY KEY (`id`),
            UNIQUE KEY `tenant_id` (`tenant_id`)
        ) {$charset};";

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