<?php

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

/**
 * Phase 2 — technician competence (certifications with expiry alerts) and parts
 * storage structure (bins/zones/shelves within an ERP location, plus an optional
 * product → bin map for pick locations).
 */
return new class extends Migration {
    public function up(): void
    {
        if (! Schema::hasTable('vrc_technician_certifications')) {
            Schema::create('vrc_technician_certifications', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('technician_id');
                $t->string('name', 191);
                $t->string('cert_no', 120)->nullable();
                $t->string('issuer', 191)->nullable();
                $t->date('issued_on')->nullable();
                $t->date('expires_on')->nullable();
                $t->string('document_path', 255)->nullable();
                $t->string('status', 20)->default('valid'); // valid|expiring|expired
                $t->text('notes')->nullable();
                $t->timestamps();
                $t->index(['business_id', 'technician_id'], 'vrc_tech_cert_biz_tech_idx');
                $t->index(['business_id', 'expires_on'], 'vrc_tech_cert_biz_exp_idx');
            });
        }

        if (! Schema::hasTable('vrc_storage_bins')) {
            Schema::create('vrc_storage_bins', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedInteger('location_id')->nullable();
                $t->string('code', 60);
                $t->string('zone', 60)->nullable();
                $t->string('rack', 60)->nullable();
                $t->string('shelf', 60)->nullable();
                $t->string('description', 191)->nullable();
                $t->boolean('is_active')->default(true);
                $t->timestamps();
                $t->index(['business_id', 'is_active'], 'vrc_storage_bins_biz_active_idx');
                $t->unique(['business_id', 'code'], 'vrc_storage_bins_biz_code_uniq');
            });
        }

        if (! Schema::hasTable('vrc_part_bin_locations')) {
            Schema::create('vrc_part_bin_locations', function (Blueprint $t) {
                $t->bigIncrements('id');
                $t->unsignedInteger('business_id');
                $t->unsignedBigInteger('bin_id');
                $t->unsignedInteger('erp_product_id');
                $t->unsignedInteger('erp_variation_id')->nullable();
                $t->string('note', 191)->nullable();
                $t->timestamps();
                $t->index(['business_id', 'bin_id'], 'vrc_part_bin_biz_bin_idx');
                $t->index(['business_id', 'erp_product_id'], 'vrc_part_bin_biz_prod_idx');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('vrc_part_bin_locations');
        Schema::dropIfExists('vrc_storage_bins');
        Schema::dropIfExists('vrc_technician_certifications');
    }
};
