<?php

namespace Database\Factories;

use App\Models\Client;
use App\Models\Loan;
use App\Models\LoanProduct;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Loan>
 */
class LoanFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $amount = $this->faker->numberBetween(1000, 50000);
        $interestRate = $this->faker->randomFloat(2, 5, 25);
        $duration = $this->faker->numberBetween(6, 60);
        $monthlyPayment = ($amount * (1 + ($interestRate / 100))) / $duration;
        $totalRepayment = $monthlyPayment * $duration;

        return [
            'loan_number' => 'LN-' . $this->faker->unique()->numberBetween(1000, 9999),
            'client_id' => Client::factory(),
            'loan_product_id' => LoanProduct::factory(),
            'loan_type' => $this->faker->randomElement(['cash', 'product']),
            'amount' => $amount,
            'interest_rate' => $interestRate,
            'duration_months' => $duration,
            'purpose' => $this->faker->sentence(),
            'status' => $this->faker->randomElement([
                'pending', 'processing', 'approved', 'rejected',
                'disbursed', 'arrears', 'defaulted', 'completed'
            ]),
            'application_date' => $this->faker->date(),
            'approval_date' => $this->faker->optional()->date(),
            'disbursement_date' => $this->faker->optional()->date(),
            'repayment_date' => $this->faker->date(),
            'monthly_payment' => round($monthlyPayment, 2),
            'total_repayment' => round($totalRepayment, 2),
            'outstanding_balance' => $this->faker->randomFloat(2, 0, $totalRepayment),
            'collateral_type' => $this->faker->optional()->randomElement(['land', 'vehicle', 'house', 'equipment']),
            'collateral_value' => $this->faker->optional()->numberBetween(1000, 100000),
            'guarantor_name' => $this->faker->optional()->name(),
            'guarantor_phone' => $this->faker->optional()->phoneNumber(),
            'notes' => $this->faker->optional()->sentence(),
            'created_by' => User::factory(),
            'approved_by' => User::factory(),
            'disbursed_by' => User::factory(),
        ];
    }

    /**
     * Indicate that the loan is approved.
     */
    public function approved(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'approved',
            'approval_date' => $this->faker->date(),
        ]);
    }

    /**
     * Indicate that the loan is disbursed.
     */
    public function disbursed(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'disbursed',
            'disbursement_date' => $this->faker->date(),
        ]);
    }

    /**
     * Indicate that the loan is in arrears.
     */
    public function arrears(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'arrears',
        ]);
    }
}
