<?php

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

/**
 * Phase 4 — ISO 9001 quality management.
 *  - vrc_documents + vrc_document_acknowledgements: controlled SOP / work-
 *    instruction register with versioning, approval and read-acknowledgement.
 *  - vrc_ncrs: non-conformance reports (from complaints, incidents, audits).
 *  - vrc_capas: corrective / preventive actions with owner, due date, closure
 *    and effectiveness review — the continuous-improvement loop.
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_documents')) {
            Schema::create('vrc_documents', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('code', 60);
                $t->string('title', 191);
                $t->string('category', 60)->nullable(); // sop|work_instruction|policy|form|manual
                $t->string('version', 20)->default('1.0');
                $t->string('status', 20)->default('draft'); // draft|under_review|approved|obsolete
                $t->string('file_path', 255)->nullable();
                $t->longText('content')->nullable();
                $t->unsignedInteger('owner_id')->nullable();
                $t->unsignedInteger('approved_by')->nullable();
                $t->dateTime('approved_at')->nullable();
                $t->date('review_due')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status'], 'vrc_documents_biz_status_idx');
                $t->index(['business_id', 'code'], 'vrc_documents_biz_code_idx');
            });
        }

        if (! Schema::hasTable('vrc_document_acknowledgements')) {
            Schema::create('vrc_document_acknowledgements', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('document_id');
                $t->unsignedInteger('user_id');
                $t->string('version', 20);
                $t->dateTime('acknowledged_at');
                $t->timestamp('created_at')->nullable();
                $t->index(['business_id', 'document_id'], 'vrc_doc_ack_biz_doc_idx');
            });
        }

        if (! Schema::hasTable('vrc_ncrs')) {
            Schema::create('vrc_ncrs', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('number', 60)->nullable();
                $t->string('source', 20)->default('internal'); // complaint|incident|audit|internal|supplier
                $t->string('title', 191);
                $t->text('description')->nullable();
                $t->string('severity', 20)->default('minor'); // minor|major|critical
                $t->string('status', 20)->default('open'); // open|investigating|action_planned|closed
                $t->unsignedBigInteger('repair_order_id')->nullable();
                $t->unsignedBigInteger('ticket_id')->nullable();
                $t->unsignedInteger('raised_by')->nullable();
                $t->dateTime('raised_at')->nullable();
                $t->dateTime('closed_at')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status'], 'vrc_ncrs_biz_status_idx');
            });
        }

        if (! Schema::hasTable('vrc_capas')) {
            Schema::create('vrc_capas', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->string('number', 60)->nullable();
                $t->unsignedBigInteger('ncr_id')->nullable();
                $t->unsignedBigInteger('incident_id')->nullable();
                $t->string('type', 20)->default('corrective'); // corrective|preventive
                $t->string('title', 191);
                $t->text('root_cause')->nullable();
                $t->text('action_plan')->nullable();
                $t->unsignedInteger('owner_id')->nullable();
                $t->date('due_date')->nullable();
                $t->string('status', 20)->default('open'); // open|in_progress|verifying|closed
                $t->dateTime('closed_at')->nullable();
                $t->text('effectiveness')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'status'], 'vrc_capas_biz_status_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_capas');
        Schema::dropIfExists('vrc_ncrs');
        Schema::dropIfExists('vrc_document_acknowledgements');
        Schema::dropIfExists('vrc_documents');
    }
};
