<?php

namespace App\Services\Semesters;

use App\Models\Configuration;
use DateTimeImmutable;

class SemesterConfigService
{
    public function getFallStart(): ?DateTimeImmutable
    {
        return $this->parseDate(Configuration::getConfigValueByKey('fall_semester_start'));
    }

    public function getSpringStart(): ?DateTimeImmutable
    {
        return $this->parseDate(Configuration::getConfigValueByKey('spring_semester_start'));
    }

    public function getLastSchoolDay(): ?DateTimeImmutable
    {
        $value = Configuration::getConfigValueByKey('last_school_day')
            ?? Configuration::getConfigValueByKey('last_day_of_school');

        return $this->parseDate($value);
    }

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

        try {
            return new DateTimeImmutable($value);
        } catch (\Throwable) {
            return null;
        }
    }
}
