<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('investors', function (Blueprint $table) {
            $table->id();
            $table->string('investor_number')->unique();
            $table->enum('entity_type', ['individual', 'company']);

            // Personal / Company details
            $table->string('name');
            $table->string('company_name')->nullable();
            $table->string('email')->nullable();
            $table->string('phone')->nullable();
            $table->string('address')->nullable();
            $table->string('country')->nullable();
            $table->string('nationality')->nullable();
            $table->date('date_of_birth')->nullable();
            $table->string('id_number')->nullable();       // NIN / passport / RC number
            $table->string('id_type')->nullable();         // passport | nin | rc_number | tin
            $table->string('photo_path')->nullable();
            $table->string('id_document_path')->nullable();

            // Investment details
            $table->enum('investment_type', ['money', 'assets', 'intellectual_property', 'mixed']);
            $table->decimal('money_amount', 20, 2)->default(0);
            $table->text('asset_description')->nullable();
            $table->decimal('asset_value', 20, 2)->default(0);
            $table->text('intellectual_description')->nullable();
            $table->decimal('intellectual_value', 20, 2)->default(0);
            $table->decimal('total_investment_value', 20, 2)->default(0);

            // Equity / Returns
            $table->decimal('equity_percentage', 8, 4)->default(0);
            $table->bigInteger('shares_count')->default(0);
            $table->decimal('share_price', 20, 2)->default(0);
            $table->decimal('expected_dividend_rate', 8, 4)->default(0); // % per period
            $table->enum('dividend_frequency', ['monthly','quarterly','annually','on_exit'])->default('annually');

            // Company they invest in
            $table->foreignId('company_id')->nullable()->constrained('companies')->onDelete('set null');

            // Status & metadata
            $table->enum('status', ['active','inactive','exited','pending'])->default('pending');
            $table->enum('partner_type', ['investor','partner','angel','venture_capital','strategic'])->default('investor');
            $table->date('investment_date')->nullable();
            $table->date('exit_date')->nullable();
            $table->text('notes')->nullable();

            $table->foreignId('recorded_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
            $table->softDeletes();
        });

        Schema::create('investor_payments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('investor_id')->constrained('investors')->onDelete('cascade');
            $table->enum('payment_type', ['dividend','share_sale','profit_distribution','buyback','bonus','other']);
            $table->decimal('amount', 20, 2);
            $table->date('payment_date');
            $table->string('reference_number')->nullable();
            $table->string('payment_method')->default('bank_transfer');
            $table->text('notes')->nullable();
            $table->string('receipt_path')->nullable();
            $table->foreignId('recorded_by')->nullable()->constrained('users')->onDelete('set null');
            $table->timestamps();
        });
    }

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