<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('main_organizations', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('organization_id');
            $table->string('role')->comment('cooperative, ngo, corporation, sponsor, etc.');
            $table->unsignedSmallInteger('priority')->default(0)->comment('0 = primary; higher values = secondary mains in same role');
            $table->boolean('is_active')->default(true);
            $table->json('metadata')->nullable()->comment('display name overrides, branding, tagline');
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();
            $table->timestamps();

            $table->foreign('organization_id')->references('id')->on('organizations')->onDelete('cascade');
            $table->index(['role', 'is_active']);
            $table->index('organization_id');
            $table->unique(['role', 'priority', 'is_active'], 'main_orgs_role_priority_active_unique');
        });
    }

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