<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * One row per tenant account. The platform itself (ABIC_E-Learning) is
     * seeded as the first, "root" company so existing single-tenant data has
     * somewhere to belong to once the rest of the schema is scoped by it.
     */
    public function up(): void
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->foreignId('logo_id')->nullable()->constrained('media')->nullOnDelete();
            // Nullable + no FK constraint here: the owning user's row itself
            // carries company_id, which would create a circular FK if this
            // pointed back with a hard constraint. Enforced in application code.
            $table->unsignedBigInteger('owner_user_id')->nullable();
            // pending: created but its subscription hasn't started yet (or
            // it has no subscription at all). active: currently within its
            // subscription's start/end window. suspended: a root-admin
            // override, independent of dates, until manually reactivated.
            // expired: its subscription's end date has passed. Only
            // 'active' allows writes — see EnforceCompanyReadOnly.
            $table->enum('status', ['pending', 'active', 'suspended', 'expired'])->default('pending');
            $table->timestamps();

            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('companies');
    }
};
