<?php

declare(strict_types=1);

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->string('type', 16)->default('agency')->after('status');
            $table->unsignedBigInteger('owner_user_id')->nullable()->after('type');
            $table->index('type');
        });

        Schema::create('organization_settings', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id')->unique();
            $table->string('trade_name')->nullable();
            $table->string('timezone', 64)->default('America/Sao_Paulo');
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
        });

        Schema::create('subscription_plans', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->string('code', 32)->unique();
            $table->string('name');
            $table->unsignedInteger('max_users')->default(1);
            $table->unsignedInteger('max_properties')->default(50);
            $table->unsignedInteger('max_whatsapp_messages_month')->default(2000);
            $table->boolean('is_active')->default(true);
            $table->timestamps();
        });

        Schema::create('organization_subscriptions', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id')->unique();
            $table->uuid('subscription_plan_id');
            $table->string('status', 32)->default('trialing');
            $table->timestamp('trial_ends_at')->nullable();
            $table->timestamp('current_period_ends_at')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->foreign('subscription_plan_id')->references('id')->on('subscription_plans')->restrictOnDelete();
        });

        Schema::create('organization_invitations', function (Blueprint $table): void {
            $table->uuid('id')->primary();
            $table->uuid('tenant_id');
            $table->string('email');
            $table->string('role', 32);
            $table->string('token', 64)->unique();
            $table->string('status', 32)->default('pending');
            $table->unsignedBigInteger('invited_by_user_id')->nullable();
            $table->timestamp('expires_at');
            $table->timestamp('accepted_at')->nullable();
            $table->timestamps();

            $table->foreign('tenant_id')->references('id')->on('tenants')->cascadeOnDelete();
            $table->foreign('invited_by_user_id')->references('id')->on('users')->nullOnDelete();
            $table->index(['tenant_id', 'email']);
            $table->index(['tenant_id', 'status']);
        });

        Schema::table('tenants', function (Blueprint $table): void {
            $table->foreign('owner_user_id')->references('id')->on('users')->nullOnDelete();
        });
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropForeign(['owner_user_id']);
        });

        Schema::dropIfExists('organization_invitations');
        Schema::dropIfExists('organization_subscriptions');
        Schema::dropIfExists('subscription_plans');
        Schema::dropIfExists('organization_settings');

        Schema::table('tenants', function (Blueprint $table): void {
            $table->dropIndex(['type']);
            $table->dropColumn(['type', 'owner_user_id']);
        });
    }
};
