<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('service_tickets', function (Blueprint $table) {
            $table->text('initial_diagnosis')->nullable()->after('problem_desc');
            $table->text('diagnosis_findings')->nullable()->after('initial_diagnosis');
            $table->text('probable_cause')->nullable()->after('diagnosis_findings');
            $table->text('recommended_action')->nullable()->after('probable_cause');
            $table->decimal('estimated_cost', 15, 2)->nullable()->after('recommended_action');
            $table->unsignedInteger('estimated_minutes')->nullable()->after('estimated_cost');
            $table->timestamp('diagnosis_completed_at')->nullable()->after('estimated_minutes');
            $table->foreignId('diagnosis_completed_by')->nullable()->after('diagnosis_completed_at')->constrained('users')->nullOnDelete();

            $table->text('repair_summary')->nullable()->after('repair_approved_by');
            $table->text('parts_used')->nullable()->after('repair_summary');

            $table->text('final_check_notes')->nullable()->after('parts_used');
            $table->string('final_check_photo_path')->nullable()->after('final_check_notes');
            $table->timestamp('final_check_passed_at')->nullable()->after('final_check_photo_path');
            $table->foreignId('final_check_passed_by')->nullable()->after('final_check_passed_at')->constrained('users')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('service_tickets', function (Blueprint $table) {
            $table->dropConstrainedForeignId('final_check_passed_by');
            $table->dropConstrainedForeignId('diagnosis_completed_by');
            $table->dropColumn([
                'initial_diagnosis',
                'diagnosis_findings',
                'probable_cause',
                'recommended_action',
                'estimated_cost',
                'estimated_minutes',
                'diagnosis_completed_at',
                'repair_summary',
                'parts_used',
                'final_check_notes',
                'final_check_photo_path',
                'final_check_passed_at',
            ]);
        });
    }
};
