<?php

namespace Database\Factories;

use App\Enums\PayrollStatus;
use App\Models\Employee;
use App\Models\Payroll;
use App\Models\PayrollPeriod;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Payroll>
 */
class PayrollFactory extends Factory
{
    public function definition(): array
    {
        $baseSalary = fake()->numberBetween(2_000_000, 8_000_000);
        $bonusAmount = fake()->numberBetween(0, 1_000_000);
        $deductionAmount = fake()->numberBetween(0, 500_000);

        return [
            'payroll_period_id' => PayrollPeriod::factory(),
            'employee_id' => Employee::factory(),
            'cash_transaction_id' => null,
            'base_salary' => $baseSalary,
            'bonus_amount' => $bonusAmount,
            'deduction_amount' => $deductionAmount,
            'total_amount' => max(0, $baseSalary + $bonusAmount - $deductionAmount),
            'status' => PayrollStatus::UNPAID->value,
            'paid_at' => null,
            'paid_by_id' => null,
        ];
    }

    public function paid(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => PayrollStatus::PAID->value,
            'paid_at' => now(),
        ]);
    }
}
