<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('associations', function (Blueprint $table) {
            $table->id();
            $table->uuid('uuid')->unique();
            
            // Basic Information
            $table->string('name');
            $table->string('slug')->unique();
            $table->foreignId('association_type_id')->constrained('association_types');
            $table->text('description')->nullable();
            $table->text('mission_statement')->nullable();
            $table->year('founded_year')->nullable();
            
            // Branding
            $table->string('logo_path')->nullable();
            $table->string('cover_image_path')->nullable();
            $table->string('primary_color')->default('#3B82F6');
            $table->string('secondary_color')->default('#10B981');
            
            // Contact Information
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->string('phone_alt')->nullable();
            $table->text('address')->nullable();
            $table->string('city')->nullable();
            $table->string('state')->nullable();
            $table->string('country')->default('Nigeria');
            
            // Social Media
            $table->string('website')->nullable();
            $table->string('facebook')->nullable();
            $table->string('twitter')->nullable();
            $table->string('instagram')->nullable();
            $table->string('linkedin')->nullable();
            
            // Member Subscription Mode (members pay association)
            $table->enum('membership_mode', ['free', 'paid'])->default('free');
            $table->decimal('membership_fee', 12, 2)->nullable();
            $table->enum('membership_billing_cycle', ['monthly', 'quarterly', 'annually'])->nullable();
            
            // Platform Subscription (SaaS - association pays Guild360)
            $table->enum('subscription_status', ['trial', 'active', 'past_due', 'suspended', 'cancelled'])->default('trial');
            $table->enum('subscription_plan', ['basic', 'professional', 'enterprise'])->default('basic');
            $table->decimal('subscription_amount', 12, 2)->default(0);
            $table->timestamp('subscription_starts_at')->nullable();
            $table->timestamp('subscription_ends_at')->nullable();
            $table->timestamp('trial_ends_at')->nullable();
            
            // Limits (based on subscription plan)
            $table->integer('max_members')->default(100); // Basic: 100, Pro: 500, Enterprise: unlimited (-1)
            $table->integer('current_member_count')->default(0);
            $table->integer('storage_limit_mb')->default(1000); // 1GB for basic
            
            // Status
            $table->enum('status', ['active', 'suspended', 'archived'])->default('active');
            
            // Metadata
            $table->foreignId('created_by')->constrained('users');
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('slug');
            $table->index('subscription_status');
            $table->index('subscription_ends_at');
            $table->index(['status', 'subscription_status']);
        });
    }

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