<?php

declare(strict_types=1);

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

/**
 * Create subscription requests table.
 *
 * Allows Company Admins to request changes to their subscription
 * (upgrades, limit increases, etc.) for Super Admin approval.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('subscription_requests', function (Blueprint $table) {
            $table->id();

            // ═══════════════════════════════════════════════════════════════════════
            // RELATIONSHIPS
            // ═══════════════════════════════════════════════════════════════════════
            $table->foreignId('company_id')
                ->constrained()
                ->cascadeOnDelete();

            $table->foreignId('subscription_id')
                ->nullable()
                ->constrained()
                ->nullOnDelete()
                ->comment('Current subscription when request was made');

            $table->foreignId('requested_by_user_id')
                ->constrained('users')
                ->cascadeOnDelete()
                ->comment('Company Admin who made the request');

            // ═══════════════════════════════════════════════════════════════════════
            // REQUEST DETAILS
            // ═══════════════════════════════════════════════════════════════════════
            $table->enum('request_type', [
                'upgrade_plan',       // Change to a higher plan
                'downgrade_plan',     // Change to a lower plan
                'increase_users',     // More user slots
                'increase_collectors', // More collector slots
                'increase_credits',   // More active credits
                'increase_volume',    // Higher monthly volume
                'increase_clients',   // More client records
                'custom_feature',     // Request a specific feature
                'billing_change',     // Change billing cycle
                'other',              // Other requests
            ])->comment('Type of change requested');

            $table->foreignId('requested_plan_id')
                ->nullable()
                ->constrained('plans')
                ->nullOnDelete()
                ->comment('Plan requested (for upgrade/downgrade)');

            $table->string('current_value', 100)->nullable()
                ->comment('Current limit value');

            $table->string('requested_value', 100)->nullable()
                ->comment('Requested new value');

            $table->text('comments')
                ->comment('Company Admin explanation/justification');

            // ═══════════════════════════════════════════════════════════════════════
            // STATUS & REVIEW
            // ═══════════════════════════════════════════════════════════════════════
            $table->enum('status', [
                'pending',    // Awaiting review
                'approved',   // Approved by Super Admin
                'rejected',   // Rejected by Super Admin
                'canceled',   // Canceled by Company Admin
                'implemented', // Change has been applied
            ])->default('pending');

            $table->foreignId('reviewed_by_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('Super Admin who reviewed');

            $table->timestamp('reviewed_at')->nullable()
                ->comment('When the request was reviewed');

            $table->text('admin_notes')->nullable()
                ->comment('Super Admin response/notes');

            // ═══════════════════════════════════════════════════════════════════════
            // IMPLEMENTATION TRACKING
            // ═══════════════════════════════════════════════════════════════════════
            $table->timestamp('implemented_at')->nullable()
                ->comment('When the change was applied');

            $table->foreignId('implemented_by_user_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete();

            $table->json('implementation_details')->nullable()
                ->comment('Details of what was changed');

            // ═══════════════════════════════════════════════════════════════════════
            // METADATA
            // ═══════════════════════════════════════════════════════════════════════
            $table->unsignedTinyInteger('priority')->default(5)
                ->comment('Priority: 1=urgent, 5=normal, 10=low');

            $table->timestamp('expires_at')->nullable()
                ->comment('Request expires if not reviewed by this date');

            $table->timestamps();
            $table->softDeletes();

            // ═══════════════════════════════════════════════════════════════════════
            // INDEXES
            // ═══════════════════════════════════════════════════════════════════════
            $table->index('status');
            $table->index('request_type');
            $table->index(['company_id', 'status']);
            $table->index('created_at');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('subscription_requests');
    }
};
