<?php

namespace App\Console\Commands;

use App\Models\UserAction;
use App\Models\UserFlight;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;

#[Signature('app:flight-feed-update')]
#[Description('Command description')]
class FlightFeedUpdate extends Command
{
    public function handle()
    {
        $time = now('UTC')->startOfMinute();

        $this->logFlightActions('flight_departing', UserFlight::where('departure_date', $time)->get());
        $this->logFlightActions('flight_arriving', UserFlight::where('arrival_date', $time)->get());
    }

    /**
     * @param string $type
     * @param Collection<UserFlight> $flights
     * @return void
     */
    private function logFlightActions(string $type, Collection $flights): void
    {

        foreach ($flights as $flight) {

            UserAction::create([
                'user_id' => $flight->user_id,
                'type'    => $type,
                'data'    => [
                    'flight' => $flight->snapshot($flight->id),
                ]
            ]);

        }
    }
}
