<?php

namespace Database\Factories;

use App\Models\CashTransaction;
use App\Models\Expense;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Expense>
 */
class ExpenseFactory extends Factory
{
    public function definition(): array
    {
        $amount = fake()->numberBetween(10_000, 2_000_000);

        return [
            'cash_transaction_id' => null,
            'created_by_id' => User::factory(),
            'amount' => $amount,
            'description' => fake()->sentence(3),
        ];
    }

    public function withCashTransaction(): static
    {
        return $this->state(function (array $attributes) {
            $amount = $attributes['amount'] ?? fake()->numberBetween(10_000, 2_000_000);

            return [
                'cash_transaction_id' => CashTransaction::factory()->state([
                    'amount' => $amount,
                    'balance_after' => fake()->numberBetween($amount, $amount + 10_000_000),
                    'created_by_id' => $attributes['created_by_id'] ?? User::factory(),
                ]),
            ];
        });
    }
}
