<?php

namespace Database\Seeders;

use App\Models\CashAccount;
use App\Models\CashTransaction;
use App\Models\User;
use Illuminate\Database\Seeder;

class CashTransactionSeeder extends Seeder
{
    public function run(): void
    {
        $cashAccount = CashAccount::query()->first();
        $user = User::query()->first();

        if ($cashAccount === null || $user === null) {
            CashTransaction::factory()->count(3)->deposit()->create();

            return;
        }

        $balance = (int) $cashAccount->balance;

        foreach (range(1, 3) as $index) {
            $amount = fake()->numberBetween(50_000, 500_000);
            $balance += $amount;

            CashTransaction::factory()->deposit()->create([
                'cash_account_id' => $cashAccount->id,
                'created_by_id' => $user->id,
                'amount' => $amount,
                'balance_after' => $balance,
            ]);
        }

        $cashAccount->update(['balance' => $balance]);
    }
}
