<?php

declare(strict_types=1);

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

/**
 * Plan 5 Task 3: добавить limits JSONB в tenants.
 *
 * Используется ProjectService::create() для проверки лимита max_projects.
 * Default '{}' → (int)($tenant->limits['max_projects'] ?? 10) = 10 из сервиса.
 */
return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasColumn('tenants', 'limits')) {
            return;
        }
        Schema::table('tenants', function (Blueprint $table) {
            // limits JSONB: {"max_users":5,"max_projects":10,"api_rps":60}
            // Аналог limits в tariff_plans — per-tenant override лимитов тарифа.
            $table->jsonb('limits')->default('{}')->after('api_key_limit');
        });
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table) {
            $table->dropColumn('limits');
        });
    }
};
