<?php

namespace App\Support;

use Carbon\Carbon;
use DateTimeInterface;
use InvalidArgumentException;

class Period
{
    public function __construct(
        public DateTimeInterface $startDate,
        public DateTimeInterface $endDate,
    ) {
        if ($startDate > $endDate) {
            throw new InvalidArgumentException('Start date cannot be after end date.');
        }
    }

    public static function create(DateTimeInterface $startDate, DateTimeInterface $endDate): self
    {
        return new self($startDate, $endDate);
    }

    public static function days(int $numberOfDays): self
    {
        return new self(
            Carbon::today()->subDays($numberOfDays)->startOfDay(),
            Carbon::today(),
        );
    }

    public static function months(int $numberOfMonths): self
    {
        return new self(
            Carbon::today()->subMonths($numberOfMonths)->startOfDay(),
            Carbon::today(),
        );
    }

    public static function years(int $numberOfYears): self
    {
        return new self(
            Carbon::today()->subYears($numberOfYears)->startOfDay(),
            Carbon::today(),
        );
    }
}
