<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\Contract;
use App\Entity\Employee;
use App\Entity\EmployeeContractPeriod;
use App\Enum\ContractNature;
use DateTimeImmutable;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

final class EmployeeContractPeriodFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        $this->createPeriod(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
            $this->getReference(FixtureReferences::CONTRACT_35, Contract::class),
            '2025-01-01',
            null,
            ContractNature::CDI,
            false
        );

        $this->createPeriod(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
            $this->getReference(FixtureReferences::CONTRACT_4H, Contract::class),
            '2026-01-01',
            '2026-12-31',
            ContractNature::CDD,
            false
        );

        $this->createPeriod(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
            $this->getReference(FixtureReferences::CONTRACT_FORFAIT, Contract::class),
            '2024-01-01',
            null,
            ContractNature::CDI,
            false
        );

        $this->createPeriod(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_INTERIM, Employee::class),
            $this->getReference(FixtureReferences::CONTRACT_INTERIM, Contract::class),
            '2026-02-01',
            '2026-06-30',
            ContractNature::INTERIM,
            false
        );

        $manager->flush();
    }

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

    private function createPeriod(
        ObjectManager $manager,
        Employee $employee,
        Contract $contract,
        string $startDate,
        ?string $endDate,
        ContractNature $nature,
        bool $paidLeaveSettled
    ): void {
        $period = new EmployeeContractPeriod()
            ->setEmployee($employee)
            ->setContract($contract)
            ->setStartDate(new DateTimeImmutable($startDate))
            ->setEndDate(null === $endDate ? null : new DateTimeImmutable($endDate))
            ->setContractNature($nature)
            ->setPaidLeaveSettled($paidLeaveSettled)
        ;

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