<?php

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

/**
 * Migration: Partnership and Collaboration Portal - Partners Table
 * 
 * Creates the main partners table for managing organizational partners.
 * Supports partner profiles, categories, and public directory display.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_partners', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Associated User Account (for partner login)
            $table->foreignId('user_id')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('Associated user account for partner access');
            
            // Organization Information
            $table->string('organization_name', 191)
                  ->comment('Official organization name');
            
            $table->string('slug', 191)
                  ->unique()
                  ->comment('URL-friendly identifier');
            
            $table->string('legal_name', 191)
                  ->nullable()
                  ->comment('Legal registered name (if different)');
            
            $table->text('description')
                  ->nullable()
                  ->comment('Organization description');
            
            $table->text('mission_statement')
                  ->nullable()
                  ->comment('Organization mission');
            
            // Contact Information
            $table->string('email', 191)
                  ->comment('Primary contact email');
            
            $table->string('phone', 50)
                  ->nullable()
                  ->comment('Primary phone number');
            
            $table->string('website', 191)
                  ->nullable()
                  ->comment('Organization website');
            
            // Address
            $table->string('address_line_1', 191)
                  ->nullable();
            
            $table->string('address_line_2', 191)
                  ->nullable();
            
            $table->string('city', 100)
                  ->nullable();
            
            $table->string('state_province', 100)
                  ->nullable();
            
            $table->string('postal_code', 20)
                  ->nullable();
            
            $table->string('country', 100)
                  ->default('Kenya');
            
            // Organization Type & Category
            $table->enum('organization_type', [
                'government', 
                'ngo', 
                'private_sector', 
                'academic', 
                'healthcare', 
                'international', 
                'community_based', 
                'faith_based', 
                'other'
            ])->default('ngo')->comment('Type of organization');
            
            $table->string('category', 191)
                  ->nullable()
                  ->comment('Business category/sector');
            
            // Branding
            $table->string('logo', 191)
                  ->nullable()
                  ->comment('Organization logo path');
            
            $table->string('banner_image', 191)
                  ->nullable()
                  ->comment('Profile banner image');
            
            // Social Media
            $table->json('social_media')
                  ->nullable()
                  ->comment('Social media links (JSON)');
            
            // Partnership Details
            $table->date('partnership_start_date')
                  ->nullable()
                  ->comment('When partnership began');
            
            $table->date('partnership_end_date')
                  ->nullable()
                  ->comment('Partnership end date (if applicable)');
            
            $table->enum('partnership_status', ['active', 'inactive', 'pending', 'suspended'])
                  ->default('active')
                  ->comment('Current partnership status');
            
            $table->enum('partnership_level', ['bronze', 'silver', 'gold', 'platinum'])
                  ->default('bronze')
                  ->comment('Partnership tier level');
            
            // Services Offered (for public directory)
            $table->json('services_offered')
                  ->nullable()
                  ->comment('List of services offered');
            
            // Verification & Vetting
            $table->boolean('is_verified')
                  ->default(false)
                  ->comment('Organization verified status');
            
            $table->timestamp('verified_at')
                  ->nullable()
                  ->comment('When verified');
            
            $table->foreignId('verified_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('Admin who verified');
            
            // Directory Display
            $table->boolean('show_in_directory')
                  ->default(true)
                  ->comment('Show in public directory');
            
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Featured partner flag');
            
            // Notes (Internal)
            $table->text('internal_notes')
                  ->nullable()
                  ->comment('Internal notes (not public)');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('slug');
            $table->index('organization_type');
            $table->index('partnership_status');
            $table->index('show_in_directory');
            $table->index('is_featured');
            $table->index('is_verified');
            $table->index(['partnership_status', 'show_in_directory'], 'r_partners_directory_index');
        });
    }

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