<?php

declare(strict_types=1);

namespace App\Enum;

enum ContractType: string
{
    case FORFAIT = 'FORFAIT';
    case H35     = '35H';
    case H39     = '39H';
    case INTERIM = 'INTERIM';
    case CUSTOM  = 'CUSTOM';

    public static function resolve(?string $name, ?string $trackingMode, ?int $weeklyHours): self
    {
        if (TrackingMode::PRESENCE->value === $trackingMode) {
            return self::FORFAIT;
        }

        $normalizedName = self::normalize($name);
        if ('interim' === $normalizedName) {
            return self::INTERIM;
        }

        if (35 === $weeklyHours) {
            return self::H35;
        }

        if (39 === $weeklyHours) {
            return self::H39;
        }

        return self::CUSTOM;
    }

    private static function normalize(?string $value): string
    {
        if (null === $value) {
            return '';
        }

        $normalized = mb_strtolower(trim($value));

        return str_replace('é', 'e', $normalized);
    }
}
