<?php

namespace App\Console\Commands;

use App\Enums\FlightNotificationType;
use App\Models\UserFlight;
use App\Services\FollowerNotificationService;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;

#[Signature('app:update-departed-flights')]
#[Description('Command description')]
class UpdateDepartedFlights extends Command
{

    public function handle(): void
    {
        $now = now()->utc();

        $userFlights = UserFlight::where('departure_date', '<=', $now->toDateTimeString())
            ->where(function ($query) {
                $query->whereNull('departure_processed_at')
                    ->orWhereColumn('departure_processed_at', '<', 'departure_date');
            })
            ->get();


        foreach ($userFlights as $flight) {
            $flight->update(['departure_processed_at' => $now]);
            new FollowerNotificationService()->notifyFlightEvent($flight, FlightNotificationType::Departed);
        }
    }
}
