<?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
    {
        // 1. Tambah status baru ke enum
        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN status
            ENUM('Pickup Requested','Pending','Checking','Repairing','Ready','Picked Up','Cancelled')
            NOT NULL DEFAULT 'Pending'");

        // 2. Buat kolom device fields nullable (untuk pickup requests)
        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN device_type ENUM('Laptop','PC','Printer') NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN brand VARCHAR(255) NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN model VARCHAR(255) NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN problem_desc TEXT NULL");

        // 3. Tambah kolom pickup_note
        Schema::table('service_tickets', function (Blueprint $table) {
            $table->text('pickup_note')->nullable()->after('problem_desc');
        });
    }

    public function down(): void
    {
        // Hapus pickup_note
        Schema::table('service_tickets', function (Blueprint $table) {
            $table->dropColumn('pickup_note');
        });

        // Kembalikan NOT NULL ke device fields
        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN problem_desc TEXT NOT NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN model VARCHAR(255) NOT NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN brand VARCHAR(255) NOT NULL");

        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN device_type ENUM('Laptop','PC','Printer') NOT NULL");

        // Hapus status Pickup Requested dari enum
        DB::statement("ALTER TABLE service_tickets
            MODIFY COLUMN status
            ENUM('Pending','Checking','Repairing','Ready','Picked Up','Cancelled')
            NOT NULL DEFAULT 'Pending'");
    }
};
