<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserFlight extends Model
{
    protected $table = 'user_flights';

    protected $fillable = [
        'user_id',
        'departure_date',
        'arrival_date',
        'flight_number',
        'departure_airport_id',
        'arrival_airport_id',
        'airline_id',
        'aircraft_id',
        'aircraft_registration',
        'seat_number',
        'seat_type_id',
        'flight_class_id',
        'flight_reason_id',
        'crew_type_id',
        'note',
        'auto_update',
    ];

    protected $casts = [
        'departure_date' => 'immutable_datetime',
        'arrival_date'   => 'immutable_datetime',
        'auto_update' => 'boolean',
    ];

    public function calculateGreatCircleDistance(): float{
        $earthRadiusKm = 6371;
        [$depLat, $depLong] = [$this->departureAirport->latitude_deg, $this->departureAirport->longitude_deg];
        [$arrLat, $arrLong] = [$this->arrivalAirport->latitude_deg, $this->arrivalAirport->longitude_deg];


        $latDelta  = deg2rad($arrLat - $depLat);
        $longDelta = deg2rad($arrLong - $depLong);

        $a = sin($latDelta / 2) * sin($latDelta / 2)
            + cos(deg2rad($depLat)) * cos(deg2rad($arrLat))
            * sin($longDelta / 2) * sin($longDelta / 2);

        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));

        return $earthRadiusKm * $c;
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function departureAirport(): BelongsTo
    {
        return $this->belongsTo(Airport::class, 'departure_airport_id');
    }

    public function arrivalAirport(): BelongsTo
    {
        return $this->belongsTo(Airport::class, 'arrival_airport_id');
    }

    public function crewType(): BelongsTo
    {
        return $this->belongsTo(CrewType::class);
    }

    public function airline(): BelongsTo
    {
        return $this->belongsTo(Airline::class);
    }


    public function aircraft(): BelongsTo
    {
        return $this->belongsTo(Aircraft::class);
    }

    public function seatType(): BelongsTo
    {
        return $this->belongsTo(SeatType::class);
    }

    public function flightClass(): BelongsTo
    {
        return $this->belongsTo(FlightClass::class);
    }

    public function flightReason(): BelongsTo
    {
        return $this->belongsTo(FlightReason::class);
    }
}
