<?php

namespace App\Http\Controllers\Owner\Report\Booking\ReportSalesByPackageByMonth;

use App\Modules\Shared\Domain\DateEnd;
use App\Modules\Shared\Domain\DateStart;
use DateInterval;
use DatePeriod;

class Schedule
{

    public DatePeriod $datePeriod;
    public const MONTH_NAMES = [
        'Ene',
        'Feb',
        'Mar',
        'Abr',
        'May',
        'Jun',
        'Jul',
        'Ago',
        'Sep',
        'Oct',
        'Nov',
        'Dic'
    ];

    public function __construct(
        private string $dateStart,
        private string $dateEnd,
        private string $interval
    ) {
        $dateStart = (new DateStart($dateStart))->value() . ' 00:00:00';
        $dateEnd = (new DateEnd($dateEnd))->value() . ' 23:59:59';

        if ($interval == 'month') {
            $dateInterval = new DateInterval('P1M');
        } else {
            $dateInterval = new DateInterval('P1D');
        }
        $this->datePeriod = new \DatePeriod(new \DateTime($dateStart), $dateInterval, new \DateTime($dateEnd));
    }

    public function getPeriod(): array
    {
        $months = [];
        foreach ($this->datePeriod as $date) {
            $months[] = (object)[
                'day' => $date->format('d'),
                'month' => $date->format('m'),
                'name' => self::MONTH_NAMES[$date->format('m') - 1],
                'year' => $date->format('Y')
            ];
        }
        return $months;
    }
}
