<script setup lang="ts">
import MainLayout from "@/Layouts/MainLayout.vue";
import {Head} from "@inertiajs/vue3";
import ProfileLayout from "@/Components/FlightsGoneBy/ProfileLayout.vue";
import {Achievement, Flight, User, UserAchievement} from "@/Types/types";
import BoardingPass from "@/Components/FlightsGoneBy/BoardingPass.vue";
import AllianceLogo from "@/Components/FlightsGoneBy/AllianceLogo.vue";
import FlightMap from "@/Components/FlightsGoneBy/FlightMap.vue";
import AirlineLogo from "@/Components/FlightsGoneBy/AirlineLogo.vue";

defineOptions({ layout: MainLayout })

const props = defineProps<{
    user: User
    flight: Flight
    flightCount: number
    isFollowing: boolean
}>()
</script>

<template>
    <Head :title="`${flight.flight_number ?? user.name + '\'s Flight'}`" />
    <ProfileLayout
        :user="user"
        :is-following="isFollowing"
        :flight-count="flightCount"
        :loading="false">
        <div class="flight-profile">

            <!-- Header -->
            <div class="flight-header glass glass-border">
                <BoardingPass :showToolTips="false" style="width:100%;max-width:600px" :flight="flight"/>
            </div>

            <!-- Main grid -->
            <div class="profile-grid">

                <!-- Departure -->
                <div class="panel glass glass-border">
                    <div class="panel-label">Departure</div>
                    <div class="airport-name">{{ flight.departure_airport?.name }}</div>
                    <div class="airport-location">
                        {{ flight.departure_airport?.municipality }},
                        {{ flight.departure_airport?.region?.country?.name }}
                    </div>
                    <div class="detail-rows">
                        <div class="detail-row" v-if="flight.departure_airport?.iata_code">
                            <span class="detail-key">IATA</span>
                            <span class="detail-val mono">{{ flight.departure_airport.iata_code }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.departure_airport?.icao_code">
                            <span class="detail-key">ICAO</span>
                            <span class="detail-val mono">{{ flight.departure_airport.icao_code }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.departure_airport?.region?.name">
                            <span class="detail-key">Region</span>
                            <span class="detail-val">{{ flight.departure_airport.region.name }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.departure_date">
                            <span class="detail-key">Time</span>
                            <span class="detail-val mono">{{ flight.departure_time_display }}</span>
                        </div>
                    </div>
                </div>

                <!-- Arrival -->
                <div class="panel glass glass-border">
                    <div class="panel-label">Arrival</div>
                    <div class="airport-name">{{ flight.arrival_airport?.name }}</div>
                    <div class="airport-location">
                        {{ flight.arrival_airport?.municipality }},
                        {{ flight.arrival_airport?.region?.country?.name }}
                    </div>
                    <div class="detail-rows">
                        <div class="detail-row" v-if="flight.arrival_airport?.iata_code">
                            <span class="detail-key">IATA</span>
                            <span class="detail-val mono">{{ flight.arrival_airport.iata_code }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.arrival_airport?.icao_code">
                            <span class="detail-key">ICAO</span>
                            <span class="detail-val mono">{{ flight.arrival_airport.icao_code }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.arrival_airport?.region?.name">
                            <span class="detail-key">Region</span>
                            <span class="detail-val">{{ flight.arrival_airport.region.name }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.arrival_date">
                            <span class="detail-key">Time</span>
                            <span class="detail-val mono">{{ flight.arrival_time_display }}</span>
                        </div>
                    </div>
                </div>

                <!-- Route Map -->
                <div class="panel glass glass-border panel-map">
                    <div class="panel-label">Route</div>
                    <div class="map-placeholder">
                        <FlightMap :flights="[flight]" />
                    </div>
                    <!-- Slot for a real map library integration -->
                    <!-- <MapboxMap :departure="departureCoords" :arrival="arrivalCoords" /> -->
                </div>

                <!-- Airline -->
                <div class="panel glass glass-border" v-if="flight.airline">
                    <div class="panel-label">Airline</div>
                    <div class="airline-header">
                        <AirlineLogo :airline="flight.airline" class="airline-logo" />
                        <div>
                            <div class="airline-name">{{ flight.airline.name }}</div>
                            <div class="airport-location" v-if="flight.airline?.country?.name"><span class="fi" :class="`fi-${flight.airline.country.code.toLowerCase()}`"></span> {{ flight.airline.country.name }}</div>
                        </div>
                    </div>
                    <div class="detail-rows">
                        <div class="detail-row" v-if="flight.airline?.alliance?.name">
                            <span class="detail-key">Alliance</span>
                            <span class="detail-val"><AllianceLogo :alliance="flight.airline.alliance" />{{ flight.airline.alliance.name }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.airline?.IATA_code">
                            <span class="detail-key">IATA</span>
                            <span class="detail-val mono">{{ flight.airline.IATA_code }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.airline?.ICAO_code">
                            <span class="detail-key">ICAO</span>
                            <span class="detail-val mono">{{ flight.airline.ICAO_code }}</span>
                        </div>
                    </div>
                </div>

                <!-- Aircraft -->
                <div v-if="flight.aircraft" class="panel glass glass-border">
                    <div class="panel-label">Aircraft</div>
                    <div class="livery" v-if="flight.livery_url" :style="{ backgroundImage: `url('${flight.livery_url}')` }">
                    </div>
                    <div class="airport-name">{{ flight.aircraft?.display_name_short }}</div>
                    <div class="airport-location" v-if="flight.aircraft?.manufacturer_code">{{ flight.aircraft.manufacturer_code }}</div>
                    <div class="detail-rows">
                        <div class="detail-row" v-if="flight.aircraft?.designator">
                            <span class="detail-key">Designator</span>
                            <span class="detail-val mono">{{ flight.aircraft?.designator }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.aircraft_registration">
                            <span class="detail-key">Tail No.</span>
                            <span class="detail-val mono">{{ flight.aircraft_registration }}</span>
                        </div>
                        <div class="detail-row" v-if="flight.aircraft?.engine_type">
                            <span class="detail-key">Engines</span>
                            <span class="detail-val">{{ flight.aircraft.engine_type }}</span>
                        </div>
                    </div>
                </div>

                <!-- Flight Details -->
                <div class="panel glass glass-border panel-details">
                    <div class="panel-label">Flight Details</div>
                    <div class="details-grid">
                        <div class="detail-card" v-if="flight.flight_class">
                            <div class="detail-card-label">Class</div>
                            <div class="detail-card-value"
                                 :class="flight.flight_class?.internal_name ? `class-${flight.flight_class.internal_name}-global` : ''"
                                 style="padding: 0.25rem 0.6rem; border-radius: 4px; display: inline-block;">
                                {{ flight.flight_class?.name }}
                            </div>
                        </div>
                        <div class="detail-card" v-if="flight.seat_type">
                            <div class="detail-card-label">Seat Type</div>
                            <div class="detail-card-value">{{ flight.seat_type?.name }}</div>
                        </div>
                        <div class="detail-card" v-if="flight.seat_number">
                            <div class="detail-card-label">Seat</div>
                            <div class="detail-card-value mono">{{ flight.seat_number }}</div>
                        </div>
                        <div class="detail-card" v-if="flight.flight_reason">
                            <div class="detail-card-label">Reason</div>
                            <div class="detail-card-value">{{ flight.flight_reason?.name }}</div>
                        </div>
                        <div class="detail-card" v-if="flight.crew_type">
                            <div class="detail-card-label">Travelled As</div>
                            <div class="detail-card-value">{{ flight.crew_type?.name }}</div>
                        </div>
                        <div class="detail-card" v-if="flight.duration">
                            <div class="detail-card-label">Duration</div>
                            <div class="detail-card-value mono">{{ flight.duration_display }}</div>
                        </div>
                        <div class="detail-card" v-if="flight.distance">
                            <div class="detail-card-label">Distance</div>
                            <div class="detail-card-value mono">{{ flight.distance.toLocaleString() }} km</div>
                        </div>
                        <div class="detail-card" v-if="flight.note">
                            <div class="detail-card-label">Notes</div>
                            <div class="detail-card-value notes-text">{{ flight.note }}</div>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </ProfileLayout>
</template>

<style scoped>
.livery{
    width: 100%;
    aspect-ratio: 16/9;
    background-size: cover;
}

.flight-profile {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 1.5rem;
    font-family: 'Barlow', sans-serif;
    color: var(--text);
}

/* Header */
.flight-header {
    padding: 1.5rem 2rem;
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
}

.route-display {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.airport-code {
    font-family: 'Barlow Condensed', sans-serif;
    font-size: 2.75rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    color: var(--text);
    line-height: 1;
}

.route-line {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--accent);
    flex: 1;
    min-width: 120px;
}

.route-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    flex-shrink: 0;
}

.route-dashes {
    flex: 1;
    border-top: 1.5px dashed rgba(56, 189, 248, 0.4);
}

.plane-icon {
    width: 22px;
    height: 22px;
    color: var(--accent);
    flex-shrink: 0;
}

.flight-meta {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.875rem;
    color: var(--muted);
}

.meta-sep {
    opacity: 0.4;
}

.flight-number {
    font-family: 'Share Tech Mono', monospace;
    color: var(--accent);
}

/* Grid */
.profile-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

.panel-map {
    grid-column: span 2;
}

.panel-details {
    grid-column: span 2;
}

@media (min-width: 900px) {
    .profile-grid {
        grid-template-columns: repeat(3, 1fr);
    }

    .panel-map {
        grid-column: span 1;
    }

    .panel-details {
        grid-column: span 3;
    }
}

/* Panels */
.panel {
    border-radius: 12px;
    padding: 1.25rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.panel-label {
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 0.25rem;
    opacity: 0.8;
}

.airport-name {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
    line-height: 1.3;
}

.airport-location {
    font-size: 0.8rem;
    color: var(--muted);
    margin-bottom: 0.5rem;
}

/* Detail rows */
.detail-rows {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    margin-top: 0.5rem;
    border-top: 1px solid var(--border);
    padding-top: 0.75rem;
}

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
}

.detail-key {
    color: var(--muted);
}

.detail-val {
    color: var(--text);
    text-align: right;
}

/* Map placeholder */
.map-placeholder {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    padding: 1.5rem 0;
    min-height: 160px;
    opacity: 0.6;
}

.map-bg-icon {
    width: 64px;
    height: 64px;
    color: var(--accent);
    opacity: 0.3;
}

.map-route-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 1rem;
    font-weight: 500;
    color: var(--text);
}

.map-arrow {
    color: var(--accent);
}

.map-distance {
    font-family: 'Share Tech Mono', monospace;
    font-size: 0.8rem;
    color: var(--muted);
}

/* Airline */
.airline-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.25rem;
}

.airline-logo-placeholder {
    width: 42px;
    height: 42px;
    border-radius: 8px;
    background: var(--accent-glow);
    border: 1px solid rgba(56, 189, 248, 0.2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-family: 'Share Tech Mono', monospace;
    color: var(--accent);
    flex-shrink: 0;
}

.airline-name {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text);
}

/* Details grid */
.details-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.75rem;
    margin-top: 0.5rem;
}

.detail-card {
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.6rem 0.75rem;
}

.detail-card-label {
    font-size: 0.65rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 0.3rem;
}

.detail-card-value {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text);
}

.notes-text {
    font-size: 0.8rem;
    font-weight: 400;
    color: var(--muted);
    line-height: 1.5;
}

/* Utilities */
.mono {
    font-family: 'Share Tech Mono', monospace;
}
</style>
