<?php

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::create('tenants', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            $table->string('name', 190);
            $table->string('slug', 63)->unique();
            $table->enum('status', ['trial', 'active', 'grace', 'suspended', 'cancelled'])
                ->default('trial')->index();
            $table->timestamp('trial_ends_at')->nullable();
            $table->string('timezone', 64)->default('Asia/Baghdad');
            $table->enum('default_locale', ['ar', 'en'])->default('ar');
            $table->char('currency', 3)->default('IQD');
            $table->string('whatsapp_number', 32)->nullable(); // v2.1: contact button
            $table->string('google_review_url')->nullable();
            $table->string('logo_path')->nullable();
            $table->char('primary_color', 7)->default('#0EA5E9');
            $table->json('settings')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });

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

    public function down(): void
    {
        Schema::table('users', fn (Blueprint $t) => $t->dropForeign(['tenant_id']));
        Schema::dropIfExists('tenants');
    }
};
