<?php

namespace App\Listeners;

use App\Facades\Settings;
use App\Jobs\RefreshFilterIndexJob;
use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Foundation\Bus\DispatchesJobs;

class TriggerFilterReindex
{
    use DispatchesJobs;

    public function __construct()
    {
        //
    }

    public function handle($event): void
    {
        $this->dispatch(new RefreshFilterIndexJob($this->incrementalWindow()));
    }

    private function incrementalWindow(): CarbonInterface
    {
        $minutes = match (Settings::get('stock-sync-frequency')) {
            'every-hour' => 90,
            'every-two-hours' => 180,
            'daily' => 1500,
            default => 780,
        };

        return Carbon::now()->subMinutes($minutes);
    }
}
