<?php

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

/**
 * Migration: Partnership and Collaboration Portal - Partnerships Table
 * 
 * Tracks MOUs, agreements, and formal partnership arrangements.
 * Supports document attachments and renewal tracking.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_partnerships', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Key to Partner
            $table->foreignId('partner_id')
                  ->constrained('r_partners')
                  ->onDelete('cascade')
                  ->comment('Associated partner organization');
            
            // Partnership Details
            $table->string('title', 191)
                  ->comment('Partnership agreement title');
            
            $table->enum('agreement_type', [
                'mou', 
                'moa', 
                'contract', 
                'letter_of_intent', 
                'collaboration_agreement', 
                'sponsorship', 
                'other'
            ])->default('mou')->comment('Type of agreement');
            
            $table->text('description')
                  ->nullable()
                  ->comment('Partnership description');
            
            $table->text('objectives')
                  ->nullable()
                  ->comment('Partnership objectives');
            
            $table->text('scope')
                  ->nullable()
                  ->comment('Scope of partnership');
            
            // Duration
            $table->date('start_date')
                  ->comment('Agreement start date');
            
            $table->date('end_date')
                  ->nullable()
                  ->comment('Agreement end date (null for indefinite)');
            
            $table->boolean('auto_renew')
                  ->default(false)
                  ->comment('Auto-renewal flag');
            
            $table->unsignedInteger('renewal_period_months')
                  ->nullable()
                  ->comment('Auto-renewal period in months');
            
            // Document
            $table->string('document_path', 191)
                  ->nullable()
                  ->comment('Path to signed agreement document');
            
            $table->string('document_name', 191)
                  ->nullable()
                  ->comment('Original document name');
            
            // Value/Resources
            $table->decimal('value', 15, 2)
                  ->nullable()
                  ->comment('Partnership monetary value');
            
            $table->string('currency', 3)
                  ->default('KES')
                  ->comment('Currency code');
            
            $table->text('resources_committed')
                  ->nullable()
                  ->comment('Non-monetary resources committed');
            
            // Status
            $table->enum('status', [
                'draft', 
                'pending_signature', 
                'active', 
                'expired', 
                'terminated', 
                'under_review', 
                'renewal_pending'
            ])->default('draft')->comment('Partnership status');
            
            // Key Dates
            $table->date('signed_date')
                  ->nullable()
                  ->comment('Date agreement was signed');
            
            $table->date('review_date')
                  ->nullable()
                  ->comment('Next review date');
            
            // Responsible Person (Internal)
            $table->foreignId('manager_id')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('Internal partnership manager');
            
            // Reminders
            $table->boolean('expiry_notification_sent')
                  ->default(false)
                  ->comment('Expiry notification sent flag');
            
            // Notes
            $table->text('internal_notes')
                  ->nullable()
                  ->comment('Internal notes (confidential)');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('partner_id');
            $table->index('status');
            $table->index('start_date');
            $table->index('end_date');
            $table->index('agreement_type');
            $table->index('manager_id');
            $table->index(['status', 'end_date'], 'r_partnerships_expiry_index');
        });
    }

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