<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ -- NUCLEAR+++
// File: /home/ploi/automarketer-ai.com/public/wp-content/plugins/automarketer-ai/migrations/047_pinterest_connection.php
// Description: Creates wp_amai_pinterest_connection table for OAuth token storage - NUCLEAR+++
// Last Edited: 2026-05-27
// ==================================================
namespace AutoMarketerAI\Migrations;
defined('ABSPATH') || exit;

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

        $table      = $wpdb->prefix . 'amai_pinterest_connection';
        $collate    = $wpdb->get_charset_collate();

        // Idempotent -- only create if the table does not exist
        if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") === $table) {
            return;
        }

        $wpdb->query(
            "CREATE TABLE `{$table}` (
                `id`                    INT UNSIGNED NOT NULL AUTO_INCREMENT,
                `tenant_id`             VARCHAR(64)  NOT NULL DEFAULT 'default',
                `pinterest_user_id`     VARCHAR(128) NOT NULL DEFAULT '',
                `username`              VARCHAR(128) NOT NULL DEFAULT '',
                `display_name`          VARCHAR(255) NOT NULL DEFAULT '',
                `avatar_url`            TEXT,
                `follower_count`        INT UNSIGNED NOT NULL DEFAULT 0,
                `access_token_enc`      TEXT         NOT NULL,
                `refresh_token_enc`     TEXT,
                `token_expires_at`      DATETIME     NOT NULL,
                `refresh_expires_at`    DATETIME     NOT NULL,
                `connected_at`          DATETIME     NOT NULL,
                `last_verified_at`      DATETIME     NOT NULL,
                `status`                ENUM('active','revoked','expired') NOT NULL DEFAULT 'active',
                PRIMARY KEY (`id`),
                UNIQUE KEY `uq_tenant` (`tenant_id`)
            ) {$collate}"
        );
    }
}