<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * Creates clients table for multi-tenant architecture.
     * Each client is an organization/company that has their own admins and users.
     */
    public function up(): void
    {
        Schema::create('clients', function (Blueprint $table) {
            $table->id();
            $table->string('name');                                     // Company/Organization name
            $table->string('slug', 100)->unique();                      // URL-friendly identifier
            $table->string('email')->unique();                          // Primary contact email
            $table->string('phone', 20)->nullable();                    // Contact phone
            $table->text('address')->nullable();                        // Business address
            $table->string('logo_url', 500)->nullable();                // Logo for branding (future)

            // Status
            $table->boolean('is_active')->default(true);                // Can be disabled by SuperAdmin

            // Limits
            $table->integer('max_users')->default(10);                  // Max app users allowed
            $table->integer('max_client_admins')->default(2);           // Max client admins allowed

            // Subscription & Billing
            $table->enum('subscription_type', ['trial', 'basic', 'premium', 'enterprise'])->default('trial');
            $table->timestamp('trial_ends_at')->nullable();             // Trial expiry date
            $table->timestamp('subscription_starts_at')->nullable();    // Paid subscription start
            $table->timestamp('subscription_ends_at')->nullable();      // Paid subscription end
            $table->boolean('is_subscription_active')->default(true);   // Service active status
            $table->timestamp('renewal_notified_at')->nullable();       // When renewal reminder was sent
            $table->text('subscription_notes')->nullable();             // Payment notes by SuperAdmin

            // Tracking
            $table->unsignedBigInteger('created_by')->nullable();       // SuperAdmin who created
            $table->timestamps();

            $table->foreign('created_by')->references('id')->on('admins')->onDelete('set null');

            $table->index('slug');
            $table->index('is_active');
            $table->index('subscription_ends_at');
        });
    }

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