<?php

namespace Database\Seeders;

use App\Models\AdminUser;
use App\Models\Investment;
use App\Models\Investor;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        // Create default admin users
        $this->createAdminUsers();

        // Create sample investors and investments
        if (app()->environment('local', 'development')) {
            $this->createSampleData();
        }
    }

    /**
     * Create default admin users
     */
    protected function createAdminUsers(): void
    {
        $admins = [
            [
                'name' => 'Super Admin',
                'email' => 'admin@meatsoko.com',
                'password' => Hash::make('password123'),
                'role' => 'super_admin',
                'is_active' => true,
            ],
            [
                'name' => 'Finance Manager',
                'email' => 'finance@meatsoko.com',
                'password' => Hash::make('password123'),
                'role' => 'finance_manager',
                'is_active' => true,
            ],
            [
                'name' => 'Compliance Officer',
                'email' => 'compliance@meatsoko.com',
                'password' => Hash::make('password123'),
                'role' => 'compliance_officer',
                'is_active' => true,
            ],
        ];

        foreach ($admins as $admin) {
            AdminUser::firstOrCreate(
                ['email' => $admin['email']],
                $admin
            );
        }

        $this->command->info('Admin users created successfully.');
    }

    /**
     * Create sample investors and investments for development
     */
    protected function createSampleData(): void
    {
        // Create sample investors
        $investors = [
            [
                'email' => 'john.investor@example.com',
                'full_name' => 'John Investor',
                'phone' => '+254712345678',
                'country' => 'Kenya',
                'investor_type' => 'individual',
                'kyc_status' => 'approved',
                'risk_profile' => 'moderate',
            ],
            [
                'email' => 'jane.capital@example.com',
                'full_name' => 'Jane Capital',
                'phone' => '+1234567890',
                'country' => 'United States',
                'investor_type' => 'individual',
                'kyc_status' => 'pending',
                'risk_profile' => 'aggressive',
            ],
            [
                'email' => 'acme.investments@example.com',
                'full_name' => 'ACME Investments Ltd',
                'phone' => '+44123456789',
                'country' => 'United Kingdom',
                'investor_type' => 'company',
                'kyc_status' => 'approved',
                'risk_profile' => 'moderate',
            ],
        ];

        foreach ($investors as $investorData) {
            $investor = Investor::firstOrCreate(
                ['email' => $investorData['email']],
                $investorData
            );

            // Create sample investments for each investor
            $this->createSampleInvestments($investor);
        }

        $this->command->info('Sample data created successfully.');
    }

    /**
     * Create sample investments for an investor
     */
    protected function createSampleInvestments(Investor $investor): void
    {
        $tiers = ['growth', 'strategic', 'anchor'];
        $statuses = ['pending_payment', 'payment_confirmed', 'active'];

        foreach ($tiers as $index => $tier) {
            $amount = Investment::TIER_MINIMUMS[$tier] * (1 + ($index * 0.5));
            $status = $statuses[$index] ?? 'pending_payment';

            Investment::firstOrCreate(
                [
                    'email' => $investor->email,
                    'investment_tier' => $tier,
                ],
                [
                    'reference' => Investment::generateReference(),
                    'investor_id' => $investor->id,
                    'investor_name' => $investor->full_name,
                    'email' => $investor->email,
                    'phone' => $investor->phone,
                    'country' => $investor->country,
                    'investor_type' => $investor->investor_type,
                    'investment_tier' => $tier,
                    'investment_amount' => $amount,
                    'currency' => 'USD',
                    'investment_term' => 2,
                    'payment_status' => $status === 'pending_payment' ? 'pending' : 'confirmed',
                    'terms_accepted' => true,
                    'memorandum_read' => true,
                    'risks_understood' => true,
                    'risk_tolerance_confirmed' => true,
                    'status' => $status,
                    'payment_completed_at' => $status !== 'pending_payment' ? now()->subDays(rand(1, 30)) : null,
                ]
            );
        }

        // Update investor aggregates
        $investor->updateAggregates();
    }
}
