<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

/**
 * pc_change_reports.action started life as a MySQL ENUM('parse','enrich').
 * The AI enrichment work adds two new actions — 'ai_enrich' (aggressive run
 * audit, Phase 4) and 'ai_apply' (candidate apply audit, Phase 7) — which a
 * fixed ENUM would reject. Widen it to a plain string(16) so any action label
 * fits without further schema churn. Non-lossy: existing values are preserved.
 */
class WidenPcChangeReportsAction extends Migration
{
    public function up()
    {
        if (! Schema::hasTable('pc_change_reports')) {
            return;
        }

        // Portable-ish path: doctrine/dbal may not be installed, so use raw SQL
        // for the ENUM -> VARCHAR change on MySQL and skip elsewhere (SQLite
        // stores it as TEXT already, so nothing to do there).
        $driver = DB::connection()->getDriverName();
        if ($driver === 'mysql') {
            DB::statement("ALTER TABLE `pc_change_reports` MODIFY `action` VARCHAR(16) NOT NULL DEFAULT 'parse'");
        }
    }

    public function down()
    {
        if (! Schema::hasTable('pc_change_reports')) {
            return;
        }

        $driver = DB::connection()->getDriverName();
        if ($driver === 'mysql') {
            // Only rows using the original values survive the narrowing.
            DB::statement("ALTER TABLE `pc_change_reports` MODIFY `action` ENUM('parse','enrich') NOT NULL DEFAULT 'parse'");
        }
    }
}
