<?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('pricing_coefficients', function (Blueprint $t) {
            $t->id();
            $t->foreignId('company_id')->constrained()->cascadeOnDelete();
            $t->string('name');
            $t->decimal('multiplier', 6, 3)->default(1);   // 1.15 = +15%
            $t->json('conditions')->nullable();            // {classes:[], age_min, age_max, client_vip, urgency:[]}
            $t->unsignedSmallInteger('priority')->default(100);
            $t->boolean('stackable')->default(true);
            $t->boolean('is_active')->default(true);
            $t->timestamps();

            $t->index(['company_id', 'is_active', 'priority']);
        });

        Schema::table('vehicles', function (Blueprint $t) {
            $t->string('vehicle_class', 24)->nullable()->after('fuel');
        });

        Schema::table('clients', function (Blueprint $t) {
            $t->boolean('is_vip')->default(false)->after('status');
        });

        Schema::table('work_orders', function (Blueprint $t) {
            $t->string('urgency', 16)->default('normal')->after('status'); // normal / urgent / express
        });
    }

    public function down(): void
    {
        Schema::table('work_orders', fn (Blueprint $t) => $t->dropColumn('urgency'));
        Schema::table('clients', fn (Blueprint $t) => $t->dropColumn('is_vip'));
        Schema::table('vehicles', fn (Blueprint $t) => $t->dropColumn('vehicle_class'));
        Schema::dropIfExists('pricing_coefficients');
    }
};
