<?php

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

/**
 * Phase 2 — multi-stage work-center routing (Mechanical → Body/Paint → Electrical
 * → QC …). Service lines are routed to a work center and carry a stage status; the
 * stage-history table is the per-hand-off audit trail (who moved the job between
 * centers, when, with a note).
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_work_centers')) {
            Schema::create('vrc_work_centers', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('type', 30)->default('mechanical'); // mechanical|body_paint|electrical|diagnostics|qc|wash|other
                $t->string('color', 12)->nullable();
                $t->integer('sort_order')->default(0);
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active'], 'vrc_work_centers_biz_active_idx');
            });
        }

        if (Schema::hasTable('vrc_repair_order_services')) {
            Schema::table('vrc_repair_order_services', function (Blueprint $t) {
                if (! Schema::hasColumn('vrc_repair_order_services', 'work_center_id')) {
                    $t->unsignedBigInteger('work_center_id')->nullable()->after('assigned_technician_id');
                }
                if (! Schema::hasColumn('vrc_repair_order_services', 'stage_status')) {
                    $t->string('stage_status', 20)->default('queued')->after('work_center_id'); // queued|in_progress|done|blocked
                }
                if (! Schema::hasColumn('vrc_repair_order_services', 'stage_entered_at')) {
                    $t->dateTime('stage_entered_at')->nullable()->after('stage_status');
                }
            });
        }

        if (! Schema::hasTable('vrc_ro_stage_history')) {
            Schema::create('vrc_ro_stage_history', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('repair_order_id');
                $t->unsignedBigInteger('repair_order_service_id')->nullable();
                $t->unsignedBigInteger('from_work_center_id')->nullable();
                $t->unsignedBigInteger('to_work_center_id')->nullable();
                $t->unsignedInteger('actor_id')->nullable();
                $t->string('note', 255)->nullable();
                $t->timestamp('created_at')->nullable();
                $t->index(['business_id', 'repair_order_id'], 'vrc_ro_stage_hist_biz_ro_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_ro_stage_history');
        if (Schema::hasTable('vrc_repair_order_services')) {
            Schema::table('vrc_repair_order_services', function (Blueprint $t) {
                foreach (['stage_entered_at', 'stage_status', 'work_center_id'] as $c) {
                    if (Schema::hasColumn('vrc_repair_order_services', $c)) {
                        $t->dropColumn($c);
                    }
                }
            });
        }
        Schema::dropIfExists('vrc_work_centers');
    }
};
