<template>
  <div class="admin-routes">
    <div class="header">
        <button class="back-link" @click="router.push('/admin')">&larr; Volver al Panel</button>
        <h1>Gestionar Rutas</h1>
        <button class="add-button" @click="createRoute">
            <span class="material-icons">add</span> Nueva Ruta
        </button>
    </div>

    <!-- Route List -->
    <div v-if="!selectedRoute" class="route-list">
        <div v-for="route in routes" :key="route.id" class="route-card" @click="selectRoute(route)">
            <div class="route-info">
                <h3>{{ route.name }}</h3>
                <p>{{ route.origin_city }} &rarr; {{ route.destination_city }}</p>
                <div class="status" :class="route.status">{{ translateStatus(route.status) }}</div>
            </div>
            <span class="material-icons">chevron_right</span>
        </div>
    </div>

    <!-- Single Route Editor -->
    <div v-else class="route-editor">
        <div class="editor-header">
            <button @click="selectedRoute = null">Cerrar</button>
            <h2>Editar Ruta: {{ selectedRoute.name }}</h2>
        </div>

        <div class="route-details-form">
            <div class="form-group">
                <label>Velocidad Promedio (km/h)</label>
                <input v-model.number="selectedRoute.average_speed_kmh" @change="updateRouteDetails" type="number" placeholder="ej. 30">
            </div>
             <div class="form-group">
                <label>Estado</label>
                <select v-model="selectedRoute.status" @change="updateRouteDetails">
                    <option value="active">Activa</option>
                    <option value="inactive">Inactiva</option>
                    <option value="maintenance">Mantenimiento</option>
                </select>
            </div>
        </div>
        
        <div class="stops-section">
            <h3>Paradas y Horarios</h3>
            <div class="add-stop">
                <select v-model="newStopId">
                    <option value="">Selecciona una parada para añadir</option>
                    <option v-for="stop in availableStops" :key="stop.id" :value="stop.id">
                        {{ stop.name }}
                    </option>
                </select>
                <button @click="addStop" :disabled="!newStopId">Añadir</button>
            </div>
            
            <div class="stops-list-editor">
                <div class="stops-header">
                    <span>#</span>
                    <span>Nombre</span>
                    <span>Espera (min)</span>
                    <span>Llegada</span>
                    <span>Acciones</span>
                </div>
                <!-- We use computed enriched stops for display, but need to bind inputs to original array or handle updates -->
                <div v-for="(stop, index) in routeStops" :key="stop.id" class="stop-item">
                    <span class="stop-order">{{ index + 1 }}</span>
                    <span class="stop-name">{{ stop.name }}</span>
                    
                    <div class="stop-delay">
                         <input 
                            v-model.number="stop.stop_delay_minutes" 
                            type="number" 
                            min="0" 
                            class="delay-input" 
                            @change="updateStop(stop)"
                            placeholder="0"
                        >
                    </div>

                    <span class="stop-arrival">
                        +{{ Math.round(arrivalTimes[index] || 0) }} min
                    </span>

                    <div class="stop-actions">
                        <button @click="moveStop(stop, index, -1)" :disabled="index === 0">↑</button>
                        <button @click="moveStop(stop, index, 1)" :disabled="index === routeStops.length - 1">↓</button>
                        <button @click="removeStop(stop)" class="remove">×</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
import { routesService } from '@/services/routesService'
import { busStopsService } from '@/services/busStopsService'
import type { Route, BusStop } from '@/types'

const routes = ref<Route[]>([])
const allStops = ref<BusStop[]>([])
const selectedRoute = ref<Route | null>(null)
const routeStops = ref<BusStop[]>([])
const newStopId = ref('')

onMounted(async () => {
    routes.value = await routesService.getAllRoutes()
    allStops.value = await busStopsService.getAllBusStops()
})

const availableStops = computed(() => {
    const currentIds = new Set(routeStops.value.map((s: BusStop) => s.id))
    return allStops.value.filter((s: BusStop) => !currentIds.has(s.id))
})

const arrivalTimes = computed(() => {
    if (!selectedRoute.value || !routeStops.value.length) return []
    
    // speed is in km/h. Convert to km/min = speed / 60
    const speed = selectedRoute.value.average_speed_kmh || 30 // default 30km/h
    const speedKmPerMin = speed / 60
    
    const times: number[] = []
    let currentTime = 0 // minutes from start
    
    // First stop is at 0
    times.push(0)
    
    for (let i = 1; i < routeStops.value.length; i++) {
        const prev = routeStops.value[i-1]
        const curr = routeStops.value[i]

        if (!prev || !curr) continue
        
        // Add delay of previous stop
        currentTime += (prev.stop_delay_minutes || 0)
        
        // Calculate travel time
        const dist = haversineDistance(prev.latitude, prev.longitude, curr.latitude, curr.longitude)
        const travelTime = dist / speedKmPerMin
        
        currentTime += travelTime
        times.push(currentTime)
    }
    
    return times
})

// Haversine formula for distance in km
function haversineDistance(lat1: number, lon1: number, lat2: number, lon2: number) {
    const R = 6371; // Radius of the earth in km
    const dLat = deg2rad(lat2 - lat1);
    const dLon = deg2rad(lon2 - lon1);
    const a = 
        Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * 
        Math.sin(dLon/2) * Math.sin(dLon/2)
        ; 
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    const d = R * c; // Distance in km
    return d;
}

function deg2rad(deg: number) {
  return deg * (Math.PI/180)
}

function translateStatus(status: string) {
    const statuses: Record<string, string> = {
        'active': 'Activa',
        'inactive': 'Inactiva',
        'maintenance': 'Mantenimiento'
    }
    return statuses[status] || status
}

async function createRoute() {
    const name = prompt("Introduce el nombre de la ruta")
    if (name) {
        await routesService.createRoute({
            name,
            origin_city: 'David',
            destination_city: 'Boquete',
            status: 'active',
            color: '#000000',
            direction: 'outbound'
        })
        routes.value = await routesService.getAllRoutes()
    }
}

async function selectRoute(route: Route) {
    selectedRoute.value = route
    // Ensure route object is reactive for editing (ref is reactive, passing obj by ref is fine)
    routeStops.value = await routesService.getRouteStops(route.id)
}

async function updateRouteDetails() {
    if (!selectedRoute.value) return
    try {
        await routesService.updateRoute(selectedRoute.value.id, {
            average_speed_kmh: selectedRoute.value.average_speed_kmh,
            status: selectedRoute.value.status
        })
    } catch (e) {
        alert('Error al actualizar la ruta')
    }
}

async function updateStop(stop: BusStop) {
    if (!selectedRoute.value) return
    try {
        await routesService.updateRouteStop(selectedRoute.value.id, stop.id, {
            stop_delay_minutes: stop.stop_delay_minutes || 0
        })
    } catch (e) {
        alert('Error al actualizar el retraso de la parada')
    }
}

async function addStop() {
    if (!selectedRoute.value || !newStopId.value) return
    try {
        await routesService.addStopToRoute(selectedRoute.value.id, {
            stop_id: newStopId.value,
            stop_order: routeStops.value.length + 1
        })
        routeStops.value = await routesService.getRouteStops(selectedRoute.value.id)
        newStopId.value = ''
    } catch (e) {
        alert('Error al añadir parada')
    }
}

async function moveStop(stop: BusStop, index: number, direction: number) {
    if (!selectedRoute.value) return
    const newOrder = index + 1 + direction
    try {
        await routesService.updateRouteStop(selectedRoute.value.id, stop.id, {
            stop_order: newOrder
        })
        routeStops.value = await routesService.getRouteStops(selectedRoute.value.id)
    } catch (e) {
        alert('Error al mover parada')
    }
}

async function removeStop(_stop: BusStop) {
    alert('Borrar no implementado aún')
}
</script>

<style scoped>
.admin-routes {
    padding: 32px 24px;
    max-width: 1200px;
    margin: 0 auto;
    min-height: 100vh;
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 48px;
    gap: 24px;
}

.back-link {
    background: var(--hover-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 12px 20px;
    border-radius: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    gap: 8px;
}

.back-link:hover {
    background: var(--active-bg);
    border-color: var(--active-color);
    color: var(--active-color);
    transform: translateX(-4px);
}

h1 {
    font-size: clamp(1.5rem, 4vw, 2.5rem);
    font-weight: 900;
    margin: 0;
    background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.04em;
    flex: 1;
    text-align: center;
}

.add-button {
    background: linear-gradient(135deg, #fee715 0%, #facc15 100%);
    color: #101820;
    border: none;
    padding: 12px 24px;
    border-radius: 14px;
    font-weight: 900;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 10px 20px rgba(254, 231, 21, 0.15);
}

.add-button:hover {
    transform: translateY(-4px);
    box-shadow: 0 15px 30px rgba(254, 231, 21, 0.25);
}

.add-button .material-icons {
    font-size: 20px;
}

/* Route List */
.route-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 24px;
}

.route-card {
    background: var(--card-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-radius: 24px;
    padding: 24px;
    border: 1px solid var(--border-color);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.route-card:hover {
    transform: translateY(-8px);
    border-color: var(--active-color);
    box-shadow: 0 20px 40px rgba(0,0,0,0.4);
}

.route-info h3 {
    margin: 0 0 8px;
    font-size: 1.25rem;
    font-weight: 900;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.route-info p {
    margin: 0 0 16px;
    color: var(--text-secondary);
    font-weight: 500;
}

.status {
    display: inline-flex;
    padding: 6px 12px;
    border-radius: 10px;
    font-size: 0.75rem;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.status.active { background: rgba(34, 197, 94, 0.1); color: #4ade80; border: 1px solid rgba(34, 197, 94, 0.2); }
.status.inactive { background: rgba(239, 68, 68, 0.1); color: #f87171; border: 1px solid rgba(239, 68, 68, 0.2); }
.status.maintenance { background: rgba(245, 158, 11, 0.1); color: #fbbf24; border: 1px solid rgba(245, 158, 11, 0.2); }

.route-card .material-icons {
    color: var(--text-secondary);
    transition: transform 0.3s;
}

.route-card:hover .material-icons {
    color: var(--active-color);
    transform: translateX(4px);
}

/* Editor Styles */
.route-editor {
    background: var(--card-bg);
    backdrop-filter: blur(16px);
    border-radius: 32px;
    padding: 40px;
    border: 1px solid var(--border-color);
    animation: slideIn 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes slideIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.editor-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 32px;
}

.editor-header h2 {
    font-size: 1.75rem;
    font-weight: 900;
    color: var(--text-primary);
    margin: 0;
}

.editor-header button {
    background: var(--hover-bg);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: 12px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
}

.editor-header button:hover {
    background: rgba(239, 68, 68, 0.1);
    color: #f87171;
    border-color: #f87171;
}

.route-details-form {
    background: var(--bg-secondary);
    padding: 24px;
    border-radius: 20px;
    margin-bottom: 40px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 24px;
    border: 1px solid var(--border-color);
}

.form-group label {
    font-size: 0.85rem;
    font-weight: 800;
    color: var(--active-color);
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    display: block;
}

.form-group input, .form-group select {
    width: 100%;
    padding: 14px 18px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    outline: none;
    transition: all 0.3s;
}

.form-group input:focus, .form-group select:focus {
    border-color: var(--active-color);
    background: rgba(255, 255, 255, 0.08);
}

.stops-section h3 {
    font-size: 1.4rem;
    font-weight: 900;
    margin-bottom: 24px;
}

.add-stop {
    display: flex;
    gap: 16px;
    margin-bottom: 32px;
}

.add-stop select { flex: 1; }

.add-stop button {
    background: var(--active-color);
    color: #101820;
    border: none;
    padding: 0 24px;
    border-radius: 14px;
    font-weight: 900;
    cursor: pointer;
    transition: all 0.3s;
}

.add-stop button:disabled { opacity: 0.5; cursor: not-allowed; }

.stops-list-editor {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.stops-header {
    display: grid;
    grid-template-columns: 60px 1fr 120px 120px 150px;
    padding: 12px 24px;
    color: var(--text-secondary);
    font-weight: 800;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.stop-item {
    display: grid;
    grid-template-columns: 60px 1fr 120px 120px 150px;
    align-items: center;
    padding: 20px 24px;
    background: var(--bg-secondary);
    border-radius: 20px;
    gap: 16px;
    border: 1px solid var(--border-color);
    transition: all 0.3s;
}

.stop-item:hover {
    border-color: var(--active-color);
    transform: translateX(8px);
}

.stop-order { font-weight: 900; color: var(--active-color); font-size: 1.1rem; }
.stop-name { font-weight: 700; color: var(--text-primary); }
.stop-arrival { font-weight: 800; color: var(--text-secondary); }

.delay-input {
    width: 80px !important;
    padding: 8px 12px !important;
}

.stop-actions {
    display: flex;
    gap: 8px;
}

.stop-actions button {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background: var(--hover-bg);
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.stop-actions button:hover:not(:disabled) {
    background: var(--active-color);
    color: #101820;
    border-color: var(--active-color);
}

.stop-actions button.remove:hover {
    background: #f87171;
    border-color: #f87171;
    color: white;
}

@media (max-width: 900px) {
    .header { flex-direction: column; align-items: stretch; text-align: center; }
    .back-link { justify-content: center; }
    .add-button { justify-content: center; }
    .route-details-form { grid-template-columns: 1fr; }
    .stops-header { display: none; }
    .stop-item { grid-template-columns: 1fr; text-align: center; justify-items: center; }
    .stop-actions { margin-top: 12px; }
}
</style>
