<?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('investments', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('reference', 50)->unique();
            $table->foreignUuid('investor_id')->nullable()->constrained('investors')->nullOnDelete();
            $table->string('investor_name');
            $table->string('email');
            $table->string('phone', 50)->nullable();
            $table->string('country', 100)->nullable();
            $table->enum('investor_type', ['individual', 'company', 'fund', 'family_office', 'institution'])->default('individual');
            $table->enum('investment_tier', ['growth', 'strategic', 'anchor']);
            $table->decimal('investment_amount', 12, 2);
            $table->string('currency', 3)->default('USD');
            $table->integer('investment_term')->nullable()->comment('In years');
            $table->enum('payment_status', ['pending', 'processing', 'confirmed', 'failed', 'refunded'])->default('pending');
            $table->string('payment_reference', 100)->unique()->nullable();
            $table->string('payment_method', 50)->nullable();
            $table->boolean('terms_accepted')->default(false);
            $table->boolean('memorandum_read')->default(false);
            $table->boolean('risks_understood')->default(false);
            $table->boolean('risk_tolerance_confirmed')->default(false);
            $table->boolean('newsletter_subscribed')->default(false);
            $table->boolean('call_requested')->default(false);
            $table->timestamp('payment_completed_at')->nullable();
            $table->timestamp('agreement_sent_at')->nullable();
            $table->timestamp('agreement_signed_at')->nullable();
            $table->enum('status', ['pending_payment', 'payment_confirmed', 'agreement_pending', 'active', 'completed', 'cancelled'])->default('pending_payment');
            $table->text('notes')->nullable();
            $table->string('ip_address', 45)->nullable();
            $table->text('user_agent')->nullable();
            $table->string('agreement_document_url')->nullable();
            $table->timestamps();
        });

        Schema::table('investments', function (Blueprint $table) {
            $table->index('email', 'idx_email');
            $table->index('reference', 'idx_reference');
            $table->index('payment_status', 'idx_payment_status');
            $table->index('status', 'idx_status');
        });
    }

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