<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\Absence;
use App\Entity\AbsenceType;
use App\Entity\Employee;
use App\Enum\HalfDay;
use DateTime;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

final class AbsenceFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        $this->createAbsence(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
            $this->getReference(FixtureReferences::ABSENCE_TYPE_CONGE, AbsenceType::class),
            '2026-03-03',
            HalfDay::AM,
            '2026-03-03',
            HalfDay::PM,
            'CP standard non forfait'
        );

        $this->createAbsence(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
            $this->getReference(FixtureReferences::ABSENCE_TYPE_CONGE, AbsenceType::class),
            '2026-03-04',
            HalfDay::AM,
            '2026-03-04',
            HalfDay::PM,
            'CP employe 4h'
        );

        $this->createAbsence(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
            $this->getReference(FixtureReferences::ABSENCE_TYPE_AUTRE, AbsenceType::class),
            '2026-03-05',
            HalfDay::AM,
            '2026-03-05',
            HalfDay::AM,
            'Absence forfait demi-journee'
        );

        $this->createAbsence(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_INTERIM, Employee::class),
            $this->getReference(FixtureReferences::ABSENCE_TYPE_ABSENT, AbsenceType::class),
            '2026-03-06',
            HalfDay::AM,
            '2026-03-06',
            HalfDay::PM,
            'Absence interim'
        );

        $manager->flush();
    }

    public function getDependencies(): array
    {
        return [
            EmployeeFixtures::class,
            AbsenceTypeFixtures::class,
        ];
    }

    private function createAbsence(
        ObjectManager $manager,
        Employee $employee,
        AbsenceType $type,
        string $startDate,
        HalfDay $startHalf,
        string $endDate,
        HalfDay $endHalf,
        string $comment
    ): void {
        $absence = new Absence()
            ->setEmployee($employee)
            ->setType($type)
            ->setStartDate(new DateTime($startDate))
            ->setStartHalf($startHalf)
            ->setEndDate(new DateTime($endDate))
            ->setEndHalf($endHalf)
            ->setComment($comment)
        ;

        $manager->persist($absence);
    }
}
