<?php

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

/**
 * Phase 4 — ISO 45001 occupational health & safety.
 *  - vrc_hazards: hazard identification & risk assessment (likelihood × severity
 *    → risk level, controls, residual risk, review date).
 *  - vrc_incidents: accident / near-miss / illness reporting & investigation,
 *    linkable to a CAPA.
 *  - vrc_safety_trainings: per-technician/user safety training & certification
 *    records with expiry.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_hazards')) {
            Schema::create('vrc_hazards', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->string('area', 120)->nullable();
                $t->string('hazard', 191);
                $t->text('risk_description')->nullable();
                $t->unsignedTinyInteger('likelihood')->default(1); // 1-5
                $t->unsignedTinyInteger('severity')->default(1);   // 1-5
                $t->unsignedSmallInteger('risk_score')->default(1);
                $t->string('risk_level', 12)->default('low'); // low|medium|high|extreme
                $t->text('controls')->nullable();
                $t->string('residual_level', 12)->nullable();
                $t->unsignedInteger('owner_id')->nullable();
                $t->date('review_due')->nullable();
                $t->string('status', 20)->default('open');
                $t->timestamps();
                $t->index(['business_id', 'risk_level'], 'vrc_hazards_biz_level_idx');
            });
        }

        if (! Schema::hasTable('vrc_incidents')) {
            Schema::create('vrc_incidents', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->string('number', 60)->nullable();
                $t->string('type', 20)->default('near_miss'); // accident|near_miss|illness|property_damage|environmental
                $t->dateTime('occurred_at');
                $t->unsignedInteger('reported_by')->nullable();
                $t->string('persons_involved', 191)->nullable();
                $t->text('description');
                $t->text('injury_details')->nullable();
                $t->text('immediate_action')->nullable();
                $t->string('status', 20)->default('reported'); // reported|investigating|action|closed
                $t->text('root_cause')->nullable();
                $t->text('investigation')->nullable();
                $t->unsignedBigInteger('capa_id')->nullable();
                $t->dateTime('closed_at')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status'], 'vrc_incidents_biz_status_idx');
            });
        }

        if (! Schema::hasTable('vrc_safety_trainings')) {
            Schema::create('vrc_safety_trainings', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('technician_id')->nullable();
                $t->unsignedInteger('user_id')->nullable();
                $t->string('course', 191);
                $t->string('provider', 191)->nullable();
                $t->date('completed_on')->nullable();
                $t->date('expires_on')->nullable();
                $t->string('certificate_path', 255)->nullable();
                $t->string('status', 20)->default('valid'); // valid|expiring|expired
                $t->text('notes')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'expires_on'], 'vrc_safety_train_biz_exp_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_safety_trainings');
        Schema::dropIfExists('vrc_incidents');
        Schema::dropIfExists('vrc_hazards');
    }
};
