<?php


namespace App\Services;


use App\Batch;
use App\StationRuleBatch;
use App\StationType;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;

class StationRuleBatchService
{
    function getByBatch(Batch $batch): StationRuleBatch
    {
        $batchType = $batch['type'] ?? 'null';
        $ownerId = $batch['owner_id'] ?? 'null';
        return Cache::remember('stationType_type_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function($batch){
            return StationRuleBatch::query()->with('stationType')->where('batch_type',$batch['type'])
                ->where('owner_id',$batch['owner_id'])
                ->first();
        });
    }

    function getStationType_toBeTask(Batch $batch): ?StationType{
        $stationRuleBatch=$this->getByBatch($batch);
        if(!$stationRuleBatch)return null;
        return $stationRuleBatch['stationType'];
    }

    /**
     * @param Batch[] $batches
     * @return Collection
     */
    function getBatches_canProcess(array $batches): Collection
    {
        $batches_toProcess=collect();
        foreach ($batches as $batch){
            $stationRuleBatch=$this->getByBatch($batch);
            if($stationRuleBatch)
            $batches_toProcess->push($batch);
        }
        return $batches_toProcess;
    }
}
