<?php

namespace App\Services\Reports;

use App\Models\ReportSchedule;
use Carbon\CarbonImmutable;

class ReportPeriodResolver
{
    /**
     * @return array{0: \Carbon\CarbonImmutable, 1: \Carbon\CarbonImmutable}
     */
    public function resolve(ReportSchedule $schedule): array
    {
        $timezone = $schedule->timezone ?: config('app.timezone', 'Europe/Istanbul');
        $now = CarbonImmutable::now($timezone);

        return match ($schedule->frequency) {
            'daily' => [$now->subDay()->startOfDay(), $now->subDay()->endOfDay()],
            'weekly' => [$now->subDays(7)->startOfDay(), $now->subDay()->endOfDay()],
            'monthly' => [$now->subMonthNoOverflow()->startOfMonth(), $now->subMonthNoOverflow()->endOfMonth()],
            default => [$now->startOfMonth(), $now->endOfDay()],
        };
    }
}
