<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('service_tickets', function (Blueprint $table) {
            $table->timestamp('repair_approved_at')->nullable()->after('cancelled_at');
            $table->foreignId('repair_approved_by')->nullable()->after('repair_approved_at')->constrained('users')->nullOnDelete();
        });

        DB::statement("ALTER TABLE service_tickets MODIFY status ENUM('Pending','Checking','Repairing','Ready','Picked Up','Cancelled') NOT NULL DEFAULT 'Pending'");
    }

    public function down(): void
    {
        DB::statement("UPDATE service_tickets SET status = 'Pending' WHERE status = 'Checking'");
        DB::statement("ALTER TABLE service_tickets MODIFY status ENUM('Pending','Repairing','Ready','Picked Up','Cancelled') NOT NULL DEFAULT 'Pending'");

        Schema::table('service_tickets', function (Blueprint $table) {
            $table->dropConstrainedForeignId('repair_approved_by');
            $table->dropColumn('repair_approved_at');
        });
    }
};
