<?php

declare(strict_types=1);

namespace Plugin\tnt_coupons\src\Helper;

use DateTimeImmutable;

/**
 * Pure, side-effect-free trigger that decides whether a sale/banner is inside its
 * configured display window.
 *
 * Boundary semantics (mirrors the coupon validity checks in Sales::setCoupon):
 *   - start is INCLUSIVE  (active when now >= start)
 *   - end   is INCLUSIVE  (active when now <= end)
 * A null bound is open-ended:
 *   - null start  => no lower bound (active from the beginning of time)
 *   - null end    => no upper bound (active until the end of time)
 *   - null/null   => always active (preserves the pre-window behaviour exactly)
 *
 * Timezone: comparisons operate on the absolute instant represented by each
 * DateTimeImmutable, so any timezone offsets carried by the arguments are honoured.
 * Callers are expected to pass values built consistently with the rest of the
 * plugin (server-local DateTimeImmutable via date_create_immutable() / new
 * DateTimeImmutable()), so all three arguments share the same clock.
 */
final class DateWindowTrigger
{
    public static function isActive(
        ?DateTimeImmutable $start,
        ?DateTimeImmutable $end,
        DateTimeImmutable $now
    ): bool {
        if ($start !== null && $now < $start) {
            return false;
        }

        if ($end !== null && $now > $end) {
            return false;
        }

        return true;
    }
}
