<?php

namespace App\Enums;

use App\Models\User;
use App\Models\UserFlight;

enum FlightNotificationType: string
{
    case Booked    = 'booked';
    case Logged    = 'logged';
    case Cancelled = 'cancelled';
    case Departed  = 'departed';
    case Arrived   = 'arrived';

    public function settingKey(): string
    {
        return match ($this) {
            self::Booked    => 'notify_on_upcoming_flight_added',
            self::Logged    => 'notify_on_historic_flight_added',
            self::Cancelled => 'notify_on_flight_cancellation',
            self::Departed  => 'notify_on_flight_departure',
            self::Arrived   => 'notify_on_flight_arrival',
        };
    }

    public function title(User $user): string
    {
        return match ($this) {
            self::Booked    => "{$user->name} booked a new flight",
            self::Logged    => "{$user->name} logged a new flight",
            self::Cancelled => "{$user->name} cancelled a flight",
            self::Departed  => "{$user->name} is taking off!",
            self::Arrived   => "{$user->name} has landed!",
        };
    }

    public function body(UserFlight $flight): string
    {
        $flightNumber = $flight->flight_number ? "{$flight->flight_number} from" : 'From';

        return match ($this) {
            self::Departed => "{$flightNumber} {$flight->departureAirport->municipality} → {$flight->arrivalAirport->municipality}",
            self::Arrived => "In {$flight->arrivalAirport->municipality}",
            default => "{$flightNumber} {$flight->departureAirport->municipality} ({$flight->departureAirport->display_code}) → "
                . "{$flight->arrivalAirport->municipality} ({$flight->arrivalAirport->display_code}) on {$flight->departure_date_display}",
        };
    }

    public function url(UserFlight $flight): string
    {
        return match ($this) {
            self::Cancelled => route('profile.view', [$flight->user->name]),
            self::Departed, self::Arrived => route('profile.flight', ['user' => $flight->user->name, 'userFlight' => $flight->id]),
            default => route('profile.departure-board', [$flight->user->name, $flight->id]),
        };
    }

    public static function forCreation(UserFlight $flight): self
    {
        return $flight->departure_date->isFuture() ? self::Booked : self::Logged;
    }
}
