<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /migrations/004_business_profile_timezone.php
// Description: Adds timezone column to amai_business_profile - NUCLEAR+++
// Last Edited: 2026-03-28
// ==================================================

namespace AutoMarketerAI\Migrations;

// No direct access
defined('ABSPATH') || exit;

final class Migration_004_BusinessProfileTimezone
{
    public const VERSION     = '004';
    public const DESCRIPTION = 'Add timezone column to amai_business_profile';

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

        $table = $wpdb->prefix . 'amai_business_profile';

        $exists = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT COUNT(*) FROM information_schema.COLUMNS
                 WHERE TABLE_SCHEMA = DATABASE()
                   AND TABLE_NAME   = %s
                   AND COLUMN_NAME  = 'timezone'",
                $table
            )
        );

        if ((int) $exists > 0) {
            return;
        }

        // Add timezone as a VARCHAR — stores a PHP/IANA timezone string
        // e.g. 'America/Toronto'. Default UTC so existing rows are safe.
        $wpdb->query(
            "ALTER TABLE {$table}
             ADD COLUMN timezone VARCHAR(64) NOT NULL DEFAULT 'UTC'
             AFTER post_time_hour"
        );
    }

    public function down(): void
    {
        // Forward-only — documents intent only. Never call in production.
        global $wpdb;
        $wpdb->query(
            "ALTER TABLE {$wpdb->prefix}amai_business_profile DROP COLUMN timezone"
        );
    }
}