<?php

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

/**
 * Maintenance module (Release 2, §5.6): any employee raises a request for a
 * hall or office — category, priority, description, photos, optional affected
 * asset. Workflow Submitted→Reviewed→Assigned→In Progress→Resolved→Verified,
 * with a status/comment update trail at every step and SLA aging.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('bms_maintenance_requests')) {
            Schema::create('bms_maintenance_requests', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('code', 40);
                $t->unsignedBigInteger('hall_id')->nullable();     // target hall (or null = office)
                $t->unsignedInteger('location_id')->nullable();    // office / business location
                $t->unsignedBigInteger('asset_id')->nullable();    // affected registered asset (optional)
                $t->string('category', 40)->default('other');      // electrical/plumbing/hvac/furniture/av/structural/cleaning/other
                $t->enum('priority', ['low', 'medium', 'high', 'urgent'])->default('medium');
                $t->string('title', 191);
                $t->text('description')->nullable();
                $t->enum('status', ['submitted', 'reviewed', 'assigned', 'in_progress', 'resolved', 'verified', 'cancelled'])->default('submitted');
                $t->unsignedInteger('reported_by')->nullable();
                $t->unsignedInteger('assigned_to')->nullable();
                $t->dateTime('due_at')->nullable();                // SLA target
                $t->dateTime('resolved_at')->nullable();
                $t->dateTime('verified_at')->nullable();
                $t->json('photos')->nullable();
                $t->timestamps();
                $t->unique(['business_id', 'code']);
                $t->index(['business_id', 'status']);
                $t->index(['business_id', 'priority']);
                $t->index(['hall_id']);
                $t->index(['assigned_to']);
            });
        }

        if (! Schema::hasTable('bms_maintenance_updates')) {
            Schema::create('bms_maintenance_updates', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('request_id');
                $t->string('from_status', 20)->nullable();
                $t->string('to_status', 20)->nullable();           // null = plain comment
                $t->text('comment')->nullable();
                $t->unsignedInteger('user_id')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'request_id']);
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('bms_maintenance_updates');
        Schema::dropIfExists('bms_maintenance_requests');
    }
};
