<?php

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

/**
 * Phase 4 — ISO 14001 environmental management.
 *  - vrc_waste_streams: catalogue of (hazardous) waste types — used oil, solvent,
 *    battery, tyre, coolant, oil filters, metal scrap …
 *  - vrc_waste_disposals: dated disposal records with quantity, vendor, manifest
 *    number and disposal certificate (the compliance audit trail).
 *  - vrc_environmental_aspects: aspects/impacts register with significance,
 *    controls and monitoring.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_waste_streams')) {
            Schema::create('vrc_waste_streams', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('name', 191);
                $t->string('name_ar', 191)->nullable();
                $t->string('type', 30)->default('other'); // used_oil|solvent|battery|tyre|coolant|oil_filter|metal_scrap|other
                $t->string('unit', 12)->default('L');
                $t->boolean('is_hazardous')->default(true);
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active'], 'vrc_waste_streams_biz_active_idx');
            });
        }

        if (! Schema::hasTable('vrc_waste_disposals')) {
            Schema::create('vrc_waste_disposals', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->unsignedBigInteger('waste_stream_id');
                $t->decimal('quantity', 12, 3)->default(0);
                $t->string('unit', 12)->default('L');
                $t->string('disposal_vendor', 191)->nullable();
                $t->string('manifest_no', 120)->nullable();
                $t->string('certificate_path', 255)->nullable();
                $t->date('disposed_on');
                $t->unsignedBigInteger('repair_order_id')->nullable();
                $t->unsignedInteger('recorded_by')->nullable();
                $t->text('notes')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'waste_stream_id'], 'vrc_waste_disp_biz_stream_idx');
                $t->index(['business_id', 'disposed_on'], 'vrc_waste_disp_biz_date_idx');
            });
        }

        if (! Schema::hasTable('vrc_environmental_aspects')) {
            Schema::create('vrc_environmental_aspects', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->string('activity', 191);
                $t->string('aspect', 191);
                $t->string('impact', 191)->nullable();
                $t->string('significance', 12)->default('low'); // low|medium|high
                $t->text('controls')->nullable();
                $t->text('monitoring')->nullable();
                $t->date('review_due')->nullable();
                $t->string('status', 20)->default('active');
                $t->timestamps();
                $t->index(['business_id', 'significance'], 'vrc_env_aspect_biz_sig_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_environmental_aspects');
        Schema::dropIfExists('vrc_waste_disposals');
        Schema::dropIfExists('vrc_waste_streams');
    }
};
