<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('diagnoses', function (Blueprint $table) {
            $table->id();
            $table->foreignId('job_card_id')->constrained()->onDelete('cascade');
            $table->foreignId('service_coordinator_id')->constrained('users')->onDelete('cascade');
            $table->text('customer_reported_issues')->nullable();
            $table->text('diagnostic_findings')->nullable();
            $table->text('root_cause_analysis')->nullable();
            $table->text('recommended_repairs')->nullable();
            $table->text('additional_issues_found')->nullable();
            $table->enum('priority_level', ['low', 'medium', 'high', 'critical'])->default('medium');
            $table->decimal('estimated_repair_time', 8, 2)->nullable();
            $table->enum('diagnosis_status', ['in_progress', 'completed', 'pending_approval', 'approved'])->default('in_progress');
            $table->timestamp('diagnosis_date')->nullable();
            $table->json('photos')->nullable();
            $table->json('diagnostic_codes')->nullable();
            $table->json('test_results')->nullable();
            $table->json('parts_required')->nullable();
            $table->json('labor_operations')->nullable();
            $table->json('special_tools_required')->nullable();
            $table->text('safety_concerns')->nullable();
            $table->text('environmental_impact')->nullable();
            $table->boolean('customer_authorization_required')->default(false);
            $table->text('notes')->nullable();
            $table->timestamps();

            $table->index(['job_card_id', 'diagnosis_status']);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('diagnoses');
    }
};
