<?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::create('injector_protocols', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
            $t->string('protocol_number', 32)->nullable();

            $t->foreignId('client_id')->nullable()->constrained('clients')->nullOnDelete();
            $t->foreignId('vehicle_id')->nullable()->constrained('vehicles')->nullOnDelete();
            $t->foreignId('work_order_id')->nullable()->constrained('work_orders')->nullOnDelete();

            // «Автосервис / Клиент» — who delivered the vehicle
            $t->string('source_type', 20)->default('client'); // 'client' | 'service_partner'
            $t->string('source_name', 160)->nullable();
            $t->string('phone', 40)->nullable();

            // vehicle snapshot at protocol time
            $t->string('car_model', 120)->nullable();
            $t->string('plate_number', 32)->nullable();
            $t->string('vin', 32)->nullable();
            $t->unsignedInteger('mileage')->nullable();
            $t->unsignedSmallInteger('year')->nullable();
            $t->string('injector_brand', 60)->nullable();
            $t->unsignedTinyInteger('injector_count')->default(8);

            // «Причина обращения»
            $t->boolean('reason_new_injectors')->default(false);
            $t->boolean('reason_engine_overhaul')->default(false);
            $t->boolean('reason_check')->default(false);

            // «Заключение»
            $t->boolean('conclusion_ok')->default(false);
            $t->boolean('conclusion_needs_cleaning')->default(false);
            $t->boolean('conclusion_needs_replacement')->default(false);
            $t->text('master_comment')->nullable();

            // Master = User with role='mechanic' (this CRM uses User, not Employee)
            $t->foreignId('master_id')->nullable()->constrained('users')->nullOnDelete();
            $t->string('master_name_signed', 120)->nullable();
            $t->date('issue_date')->nullable();
            $t->boolean('client_acknowledged')->default(false);

            $t->timestamps();

            $t->unique(['company_id', 'protocol_number']);
            $t->index(['company_id', 'plate_number']);
            $t->index(['company_id', 'vin']);
        });

        Schema::create('injector_protocol_rows', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained('companies')->cascadeOnDelete();
            $t->foreignId('injector_protocol_id')->constrained('injector_protocols')->cascadeOnDelete();
            $t->unsignedTinyInteger('position'); // 1..8

            $t->decimal('resistance_before', 6, 2)->nullable();
            $t->decimal('resistance_after', 6, 2)->nullable();
            $t->decimal('open_time_before', 6, 2)->nullable();
            $t->decimal('open_time_after', 6, 2)->nullable();
            $t->decimal('close_time_before', 6, 2)->nullable();
            $t->decimal('close_time_after', 6, 2)->nullable();
            $t->decimal('close_time_kz_before', 6, 2)->nullable();
            $t->decimal('close_time_kz_after', 6, 2)->nullable();
            $t->decimal('flow_before', 6, 2)->nullable();
            $t->decimal('flow_after', 6, 2)->nullable();
            $t->string('tightness_before', 20)->nullable();
            $t->string('tightness_after', 20)->nullable();

            $t->timestamps();

            $t->unique(['injector_protocol_id', 'position']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('injector_protocol_rows');
        Schema::dropIfExists('injector_protocols');
    }
};
