<?php

namespace Database\Factories;

use App\Models\Attendance;
use App\Models\AttendanceRegularization;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\AttendanceRegularization>
 */
class AttendanceRegularizationFactory extends Factory
{
    protected $model = AttendanceRegularization::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $date = fake()->dateTimeBetween('-30 days', '-1 day');

        return [
            'user_id' => User::factory(),
            'date' => Carbon::parse($date)->format('Y-m-d'),
            'type' => fake()->randomElement(['missing_checkin', 'missing_checkout', 'wrong_time', 'forgot_punch', 'other']),
            'reason' => fake()->sentence(),
            'status' => 'pending',
        ];
    }

    /**
     * Create a regularization for missing check-in.
     */
    public function missingCheckin(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => 'missing_checkin',
            'requested_check_in_time' => '09:00',
        ]);
    }

    /**
     * Create a regularization for missing check-out.
     */
    public function missingCheckout(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => 'missing_checkout',
            'requested_check_out_time' => '17:00',
        ]);
    }

    /**
     * Create a regularization for wrong time.
     */
    public function wrongTime(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => 'wrong_time',
            'requested_check_in_time' => '09:00',
            'requested_check_out_time' => '17:00',
        ]);
    }

    /**
     * Create a regularization for forgot punch.
     */
    public function forgotPunch(): static
    {
        return $this->state(fn (array $attributes) => [
            'type' => 'forgot_punch',
            'requested_check_in_time' => '09:00',
            'requested_check_out_time' => '17:00',
        ]);
    }

    /**
     * Create an approved regularization.
     */
    public function approved(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'approved',
            'approved_by' => User::factory(),
            'approved_at' => now(),
        ]);
    }

    /**
     * Create a rejected regularization.
     */
    public function rejected(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'rejected',
            'approved_by' => User::factory(),
            'approved_at' => now(),
            'manager_comments' => fake()->sentence(),
        ]);
    }

    /**
     * Create a regularization for a specific date.
     */
    public function forDate(string|Carbon $date): static
    {
        $dateStr = $date instanceof Carbon ? $date->format('Y-m-d') : $date;

        return $this->state(fn (array $attributes) => [
            'date' => $dateStr,
        ]);
    }

    /**
     * Create a regularization for a specific user.
     */
    public function forUser(User $user): static
    {
        return $this->state(fn (array $attributes) => [
            'user_id' => $user->id,
        ]);
    }

    /**
     * Link to an existing attendance record.
     */
    public function withAttendance(Attendance $attendance): static
    {
        return $this->state(fn (array $attributes) => [
            'attendance_id' => $attendance->id,
            'date' => $attendance->date,
            'user_id' => $attendance->user_id,
        ]);
    }
}
