<?php

declare(strict_types=1);

namespace App\Services\Alerts;

use App\Models\AlertRule;
use App\Models\Workspace;
use Carbon\CarbonImmutable;

class AlertQuietHoursService
{
    public function __construct(
        private readonly WorkspaceTimezoneResolver $workspaceTimezoneResolver,
    ) {}

    public function deferUntil(AlertRule $rule, Workspace $workspace, ?CarbonImmutable $now = null): ?CarbonImmutable
    {
        if (! (bool) $rule->quiet_hours_enabled) {
            return null;
        }

        $start = $this->parseTime((string) ($rule->quiet_hours_start ?? ''));
        $end = $this->parseTime((string) ($rule->quiet_hours_end ?? ''));

        if ($start === null || $end === null) {
            return null;
        }

        $timezone = $this->resolveTimezone($rule, $workspace);
        $localNow = ($now ?? CarbonImmutable::now($timezone))->setTimezone($timezone);

        if (! $this->isWithinQuietHours($localNow, $start, $end)) {
            return null;
        }

        $nextAllowedLocal = $this->nextAllowedAt($localNow, $start, $end);

        return $nextAllowedLocal->setTimezone('UTC');
    }

    /**
     * @param  array{hour:int,minute:int}  $start
     * @param  array{hour:int,minute:int}  $end
     */
    private function isWithinQuietHours(CarbonImmutable $localNow, array $start, array $end): bool
    {
        $nowMinutes = ((int) $localNow->format('H') * 60) + (int) $localNow->format('i');
        $startMinutes = ($start['hour'] * 60) + $start['minute'];
        $endMinutes = ($end['hour'] * 60) + $end['minute'];

        if ($startMinutes === $endMinutes) {
            return true;
        }

        if ($startMinutes < $endMinutes) {
            return $nowMinutes >= $startMinutes && $nowMinutes < $endMinutes;
        }

        return $nowMinutes >= $startMinutes || $nowMinutes < $endMinutes;
    }

    /**
     * @param  array{hour:int,minute:int}  $start
     * @param  array{hour:int,minute:int}  $end
     */
    private function nextAllowedAt(CarbonImmutable $localNow, array $start, array $end): CarbonImmutable
    {
        $nowMinutes = ((int) $localNow->format('H') * 60) + (int) $localNow->format('i');
        $startMinutes = ($start['hour'] * 60) + $start['minute'];
        $endMinutes = ($end['hour'] * 60) + $end['minute'];

        if ($startMinutes === $endMinutes) {
            return $this->atLocalTime($localNow->addDay(), $end);
        }

        if ($startMinutes < $endMinutes) {
            if ($nowMinutes >= $startMinutes && $nowMinutes < $endMinutes) {
                return $this->atLocalTime($localNow, $end);
            }

            return $localNow;
        }

        if ($nowMinutes >= $startMinutes) {
            return $this->atLocalTime($localNow->addDay(), $end);
        }

        if ($nowMinutes < $endMinutes) {
            return $this->atLocalTime($localNow, $end);
        }

        return $localNow;
    }

    /**
     * @return array{hour:int,minute:int}|null
     */
    private function parseTime(string $value): ?array
    {
        $value = trim($value);

        if (! preg_match('/^(?<hour>[01]\\d|2[0-3]):(?<minute>[0-5]\\d)$/', $value, $matches)) {
            return null;
        }

        return [
            'hour' => (int) $matches['hour'],
            'minute' => (int) $matches['minute'],
        ];
    }

    /**
     * @param  array{hour:int,minute:int}  $time
     */
    private function atLocalTime(CarbonImmutable $baseDateTime, array $time): CarbonImmutable
    {
        return $baseDateTime
            ->setTime($time['hour'], $time['minute'], 0, 0);
    }

    private function resolveTimezone(AlertRule $rule, Workspace $workspace): string
    {
        $ruleTimezone = trim((string) ($rule->quiet_hours_timezone ?? ''));

        if ($ruleTimezone !== '' && in_array($ruleTimezone, \DateTimeZone::listIdentifiers(), true)) {
            return $ruleTimezone;
        }

        return $this->workspaceTimezoneResolver->resolve($workspace);
    }
}
