<?php

namespace Database\Factories;

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

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

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

        return [
            'user_id' => User::factory(),
            'date' => Carbon::parse($date)->format('Y-m-d'),
            'status' => Attendance::STATUS_ABSENT,
        ];
    }

    /**
     * Create attendance with check-in.
     */
    public function checkedIn(): static
    {
        return $this->state(fn (array $attributes) => [
            'check_in_time' => Carbon::parse($attributes['date'])->setTime(9, 0, 0),
            'status' => Attendance::STATUS_CHECKED_IN,
        ]);
    }

    /**
     * Create attendance with check-in and check-out.
     */
    public function checkedOut(): static
    {
        return $this->state(fn (array $attributes) => [
            'check_in_time' => Carbon::parse($attributes['date'])->setTime(9, 0, 0),
            'check_out_time' => Carbon::parse($attributes['date'])->setTime(17, 0, 0),
            'status' => Attendance::STATUS_CHECKED_OUT,
            'working_hours' => 8.0,
        ]);
    }

    /**
     * Create attendance 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 attendance for a specific user.
     */
    public function forUser(User $user): static
    {
        return $this->state(fn (array $attributes) => [
            'user_id' => $user->id,
            'shift_id' => $user->shift_id,
        ]);
    }
}
