<?php

declare(strict_types=1);

namespace App\DataFixtures;

use App\Entity\Employee;
use App\Entity\EmployeeLeaveBalance;
use App\Enum\LeaveRuleCode;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

final class EmployeeLeaveBalanceFixtures extends Fixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager): void
    {
        $this->createOpeningBalance(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_STANDARD, Employee::class),
            LeaveRuleCode::CDI_CDD_NON_FORFAIT,
            2026,
            0.0,
            0.0
        );

        $this->createOpeningBalance(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_4H, Employee::class),
            LeaveRuleCode::CDI_CDD_NON_FORFAIT,
            2026,
            0.0,
            0.0
        );

        $this->createOpeningBalance(
            $manager,
            $this->getReference(FixtureReferences::EMPLOYEE_FORFAIT, Employee::class),
            LeaveRuleCode::FORFAIT_218,
            2026,
            0.0,
            0.0
        );

        $manager->flush();
    }

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

    private function createOpeningBalance(
        ObjectManager $manager,
        Employee $employee,
        LeaveRuleCode $ruleCode,
        int $year,
        float $openingDays,
        float $openingSaturdays
    ): void {
        $balance = new EmployeeLeaveBalance()
            ->setEmployee($employee)
            ->setRuleCode($ruleCode)
            ->setYear($year)
            ->setOpeningDays($openingDays)
            ->setOpeningSaturdays($openingSaturdays)
            ->setAccruedDays(0.0)
            ->setAccruedSaturdays(0.0)
            ->setTakenDays(0.0)
            ->setTakenSaturdays(0.0)
            ->setClosingDays($openingDays)
            ->setClosingSaturdays($openingSaturdays)
            ->setIsLocked(false)
        ;

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