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

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

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

        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',
                `yelp_business_id`  VARCHAR(128) NOT NULL DEFAULT '',
                `business_name`     VARCHAR(255) NOT NULL DEFAULT '',
                `business_url`      VARCHAR(512) NOT NULL DEFAULT '',
                `avatar_url`        TEXT,
                `review_count`      INT UNSIGNED NOT NULL DEFAULT 0,
                `rating`            DECIMAL(2,1) NOT NULL DEFAULT 0.0,
                `access_token_enc`  TEXT         NOT NULL,
                `token_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}"
        );
    }
}