<?php

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

/**
 * Staff licenses / credentials with a verification workflow + expiry alerts
 * (spec §5 user_management license_tracking + credentialing; REUSE-MAP §5
 * "build-new workflow on top of existing document storage" — document_path
 * stores the uploaded credential like clc_authorized_vets.document_path).
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('org_staff_licenses')) {
            return;
        }

        Schema::create('org_staff_licenses', function (Blueprint $t) {
            $t->bigIncrements('id');
            $t->unsignedInteger('business_id');
            $t->unsignedInteger('user_id');
            $t->string('license_type', 100);          // medical_license, specialization_cert, ce_credit, ...
            $t->string('license_number', 100);
            $t->string('issuing_authority', 191)->nullable();
            $t->date('issue_date')->nullable();
            $t->date('expiry_date')->nullable();
            $t->string('specialization', 150)->nullable();
            $t->enum('status', ['pending_verification', 'verified', 'rejected', 'expired'])
                ->default('pending_verification');
            $t->unsignedInteger('verified_by')->nullable();
            $t->dateTime('verified_at')->nullable();
            $t->string('document_path', 191)->nullable();
            $t->text('notes')->nullable();
            $t->timestamps();

            $t->foreign('business_id')->references('id')->on('business')->onDelete('cascade');
            $t->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $t->index(['business_id', 'user_id']);
            $t->index(['business_id', 'status']);
            $t->index(['business_id', 'expiry_date']);
        });
    }

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