<?php
declare(strict_types=1);
// ==================================================
// Nuclear Fancy Pants Edition™ — NUCLEAR+++
// File: /migrations/006_media_location_feeling.php
// Description: Adds location_place_id, location_name, feeling columns to wp_amai_scheduled_posts - NUCLEAR+++
// Last Edited: 2026-03-30
// ==================================================

namespace AutoMarketerAI\Migrations;

defined('ABSPATH') || exit;

final class Migration_006_MediaLocationFeeling
{
    public const VERSION     = '006';
    public const DESCRIPTION = 'Add location_place_id, location_name, feeling columns to scheduled posts';

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

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

        // Add location_place_id if missing
        $has_place_id = $wpdb->get_results(
            "SHOW COLUMNS FROM {$table} LIKE 'location_place_id'"
        );
        if (empty($has_place_id)) {
            $wpdb->query(
                "ALTER TABLE {$table}
                 ADD COLUMN location_place_id varchar(255) NULL DEFAULT NULL
                 AFTER post_image_url"
            );
        }

        // Add location_name if missing
        $has_location_name = $wpdb->get_results(
            "SHOW COLUMNS FROM {$table} LIKE 'location_name'"
        );
        if (empty($has_location_name)) {
            $wpdb->query(
                "ALTER TABLE {$table}
                 ADD COLUMN location_name varchar(255) NULL DEFAULT NULL
                 AFTER location_place_id"
            );
        }

        // Add feeling if missing
        $has_feeling = $wpdb->get_results(
            "SHOW COLUMNS FROM {$table} LIKE 'feeling'"
        );
        if (empty($has_feeling)) {
            $wpdb->query(
                "ALTER TABLE {$table}
                 ADD COLUMN feeling varchar(64) NULL DEFAULT NULL
                 AFTER location_name"
            );
        }
    }

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

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

        $wpdb->query("ALTER TABLE {$table} DROP COLUMN IF EXISTS feeling");
        $wpdb->query("ALTER TABLE {$table} DROP COLUMN IF EXISTS location_name");
        $wpdb->query("ALTER TABLE {$table} DROP COLUMN IF EXISTS location_place_id");
    }
}