<?php
namespace FleekDash\Database\Migrations;

use FleekDash\Database\Core\BaseMigration;

/**
 * Update Tracking Method Enum
 *
 * Add 'advanced_use' and 'external_pageview' to tracking_method enum
 * Converted to new format
 */
return new class extends BaseMigration {

    public function up(): void
    {
        // Check if update is needed
        $result = $this->wpdb->get_row(
            "SHOW COLUMNS FROM {$this->prefix}fleekdash_site_growth_events LIKE 'tracking_method'"
        );

        if (!$result || strpos($result->Type, 'advanced_use') !== false) {
            return; // Already updated
        }

        $table = $this->prefix . 'fleekdash_site_growth_events';

        // Update enum to include new values
        // ✅ SECURITY: Table and column names already use backticks (safe)
        // Enum values are hardcoded strings (safe)
        $sql = "ALTER TABLE `{$table}`
                MODIFY COLUMN `tracking_method`
                ENUM('manual', 'automatic', 'webhook', 'advanced_use', 'external_pageview')
                NOT NULL DEFAULT 'automatic'
                COMMENT 'How this event is tracked'";

        $this->wpdb->query($sql);
    }

    public function down(): void
    {
        $table = $this->prefix . 'fleekdash_site_growth_events';

        // Revert to original enum
        // ✅ SECURITY: Table and column names already use backticks (safe)
        // Enum values are hardcoded strings (safe)
        $sql = "ALTER TABLE `{$table}`
                MODIFY COLUMN `tracking_method`
                ENUM('manual', 'automatic', 'webhook')
                NOT NULL DEFAULT 'automatic'
                COMMENT 'How this event is tracked'";

        $this->wpdb->query($sql);
    }
};
