<?php

namespace Database\Seeders;

use App\Models\CashModal;
use App\Models\CashMutation;
use App\Models\Deposit;
use App\Models\PaymentAccount;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class FinanceSeeder extends Seeder
{
    public function run(): void
    {
        $this->command->info('🚀 Memulai seeding Finance data (1 Jan - 9 Feb 2026)...');
        
        $this->createCashModals();
        $this->createDeposits();
        $this->createCashMutations();
        
        $this->command->info('✅ Seeding Finance selesai!');
    }

    private function createCashModals(): void
    {
        $startDate = Carbon::create(2026, 1, 1);
        $endDate = Carbon::create(2026, 2, 9);
        $totalModals = 0;
        
        // Initial petty cash modal (1 January)
        CashModal::create([
            'amount' => 2000000,
            'modal_date' => $startDate,
            'type' => 'initial',
            'account' => 'petty',
            'notes' => 'Modal awal kas kecil tahun 2026',
            'user_id' => 1,
            'created_at' => $startDate,
            'updated_at' => $startDate,
        ]);
        $totalModals++;
        
        // Initial cash modal (1 January)
        CashModal::create([
            'amount' => 500000,
            'modal_date' => $startDate,
            'type' => 'initial',
            'account' => 'cash',
            'notes' => 'Modal awal kasir 1 Januari 2026',
            'user_id' => 1,
            'created_at' => $startDate,
            'updated_at' => $startDate,
        ]);
        $totalModals++;
        
        // Weekly cash top-ups (every Monday)
        $currentDate = $startDate->copy();
        while ($currentDate <= $endDate) {
            if ($currentDate->isMonday() && $currentDate->gt($startDate)) {
                CashModal::create([
                    'amount' => rand(3, 5) * 100000, // 300k-500k
                    'modal_date' => $currentDate->copy(),
                    'type' => 'topup',
                    'account' => 'cash',
                    'notes' => 'Tambah modal kasir minggu ke-' . $currentDate->weekOfMonth,
                    'user_id' => 1,
                    'created_at' => $currentDate,
                    'updated_at' => $currentDate,
                ]);
                $totalModals++;
            }
            $currentDate->addDay();
        }
        
        // Occasional petty cash top-up (mid-month)
        $midJan = Carbon::create(2026, 1, 15);
        CashModal::create([
            'amount' => 1000000,
            'modal_date' => $midJan,
            'type' => 'topup',
            'account' => 'petty',
            'notes' => 'Tambah kas kecil bulan Januari',
            'user_id' => 1,
            'created_at' => $midJan,
            'updated_at' => $midJan,
        ]);
        $totalModals++;
        
        $this->command->info('  💵 Cash modals seeded: ' . $totalModals);
    }

    private function createDeposits(): void
    {
        $startDate = Carbon::create(2026, 1, 1);
        $endDate = Carbon::create(2026, 2, 9);
        $totalDeposits = 0;
        
        // Get bank-type payment accounts for deposits
        $bankAccounts = PaymentAccount::where('type', 'bank')->pluck('id')->toArray();
        
        if (empty($bankAccounts)) {
            $this->command->warn('  ⚠️ No bank accounts found, skipping deposits...');
            return;
        }
        
        // Deposits typically 2-3 times per week
        $currentDate = $startDate->copy();
        while ($currentDate <= $endDate) {
            // Deposit on Tuesdays and Fridays (bank days)
            if (($currentDate->isTuesday() || $currentDate->isFriday()) && rand(0, 1)) {
                $amount = rand(20, 80) * 100000; // 2jt-8jt
                
                Deposit::create([
                    'amount' => $amount,
                    'deposit_date' => $currentDate->copy(),
                    'payment_account_id' => $bankAccounts[array_rand($bankAccounts)],
                    'reference_number' => 'DEP' . $currentDate->format('Ymd') . rand(100, 999),
                    'notes' => 'Setoran pendapatan ' . $currentDate->translatedFormat('l, d M Y'),
                    'created_by' => 1,
                    'created_at' => $currentDate,
                    'updated_at' => $currentDate,
                ]);
                $totalDeposits++;
            }
            $currentDate->addDay();
        }
        
        // Large end-of-month deposits
        $endJan = Carbon::create(2026, 1, 31);
        $bcaAccount = PaymentAccount::where('provider', 'BCA')->first();
        Deposit::create([
            'amount' => 15000000,
            'deposit_date' => $endJan,
            'payment_account_id' => $bcaAccount?->id ?? $bankAccounts[0],
            'reference_number' => 'DEP20260131001',
            'notes' => 'Setoran akhir bulan Januari 2026',
            'created_by' => 1,
            'created_at' => $endJan,
            'updated_at' => $endJan,
        ]);
        $totalDeposits++;
        
        $this->command->info('  🏦 Deposits seeded: ' . $totalDeposits);
    }

    private function createCashMutations(): void
    {
        $mutations = [
            // Transfer from cash to petty (for daily operations)
            [
                'amount' => 500000,
                'mutation_date' => Carbon::create(2026, 1, 5),
                'from_account' => 'cash',
                'to_account' => 'petty',
                'notes' => 'Transfer ke kas kecil untuk operasional harian',
            ],
            [
                'amount' => 300000,
                'mutation_date' => Carbon::create(2026, 1, 12),
                'from_account' => 'cash',
                'to_account' => 'petty',
                'notes' => 'Isi ulang kas kecil',
            ],
            [
                'amount' => 800000,
                'mutation_date' => Carbon::create(2026, 1, 20),
                'from_account' => 'cash',
                'to_account' => 'petty',
                'notes' => 'Transfer kas kecil minggu ke-3',
            ],
            // Transfer from bank to cash (withdraw for change money)
            [
                'amount' => 2000000,
                'mutation_date' => Carbon::create(2026, 1, 8),
                'from_account' => 'bank',
                'to_account' => 'cash',
                'notes' => 'Tarik tunai untuk uang kembalian',
            ],
            [
                'amount' => 1500000,
                'mutation_date' => Carbon::create(2026, 1, 22),
                'from_account' => 'bank',
                'to_account' => 'cash',
                'notes' => 'Tarik tunai untuk modal kasir',
            ],
            // February mutations
            [
                'amount' => 400000,
                'mutation_date' => Carbon::create(2026, 2, 3),
                'from_account' => 'cash',
                'to_account' => 'petty',
                'notes' => 'Transfer kas kecil awal Februari',
            ],
            [
                'amount' => 1000000,
                'mutation_date' => Carbon::create(2026, 2, 5),
                'from_account' => 'bank',
                'to_account' => 'cash',
                'notes' => 'Tarik tunai untuk operasional',
            ],
        ];
        
        foreach ($mutations as $data) {
            CashMutation::create(array_merge($data, [
                'user_id' => 1,
                'created_at' => $data['mutation_date'],
                'updated_at' => $data['mutation_date'],
            ]));
        }
        
        $this->command->info('  🔄 Cash mutations seeded: ' . count($mutations));
    }
}
