# Trip Management Script for Holiday Travel App
# Manage multiple travel plans and switch between them

$apiUrl = "http://localhost:4000"

function Show-Menu {
    Write-Host "`n=== HOLIDAY TRAVEL APP - TRIP MANAGER ===" -ForegroundColor Cyan
    Write-Host "1. View all trips" -ForegroundColor White
    Write-Host "2. View trip details" -ForegroundColor White
    Write-Host "3. Compare resorts for a trip" -ForegroundColor White
    Write-Host "4. View trips by status (Planning/Booked/Completed)" -ForegroundColor White
    Write-Host "5. Exit" -ForegroundColor Gray
    Write-Host ""
}

function Get-AllTrips {
    try {
        $response = Invoke-RestMethod -Uri "$apiUrl/api/trips" -Method Get
        
        Write-Host "`n=== YOUR TRAVEL PLANS ($($response.count) trips) ===" -ForegroundColor Cyan
        Write-Host "Planning: $($response.byStatus.planning) | Booked: $($response.byStatus.booked) | Completed: $($response.byStatus.completed)`n" -ForegroundColor Yellow
        
        foreach ($trip in $response.trips) {
            $statusColor = switch ($trip.status) {
                "planning" { "Yellow" }
                "booked" { "Green" }
                "completed" { "Gray" }
            }
            
            Write-Host "[$($trip.id)] $($trip.name)" -ForegroundColor $statusColor
            Write-Host "    Destination: $($trip.destination)" -ForegroundColor White
            Write-Host "    Region: $($trip.region)" -ForegroundColor Gray
            
            if ($trip.departureDate) {
                Write-Host "    Departure: $($trip.departureDate) ($($trip.tripLength) nights)" -ForegroundColor Gray
            }
            
            if ($trip.budget) {
                Write-Host "    Budget: $([char]0x0024)$($trip.budget) $($trip.currency)" -ForegroundColor Green
            }
            
            if ($trip.notes) {
                Write-Host "    Notes: $($trip.notes)" -ForegroundColor DarkGray
            }
            
            Write-Host ""
        }
    }
    catch {
        Write-Host "Error fetching trips: $_" -ForegroundColor Red
    }
}

function Get-TripDetails {
    param([string]$TripId)
    
    try {
        $response = Invoke-RestMethod -Uri "$apiUrl/api/trips?id=$TripId" -Method Get
        $trip = $response.trip
        
        Write-Host "`n=== $($trip.name.ToUpper()) ===" -ForegroundColor Cyan
        Write-Host "Trip #: $($trip.id)" -ForegroundColor Cyan
        Write-Host "Status: $($trip.status)" -ForegroundColor Yellow
        Write-Host "Destination: $($trip.destination)" -ForegroundColor White
        Write-Host "Region: $($trip.region)" -ForegroundColor White
        
        if ($trip.departureDate) {
            Write-Host "Travel Dates: $($trip.departureDate) ($($trip.tripLength) nights)" -ForegroundColor White
        }
        
        if ($trip.budget) {
            Write-Host "Budget: $([char]0x0024)$($trip.budget) $($trip.currency)" -ForegroundColor Green
        }
        
        if ($trip.preferences) {
            Write-Host "`nPreferences:" -ForegroundColor Cyan
            foreach ($key in $trip.preferences.PSObject.Properties.Name) {
                $value = $trip.preferences.$key
                Write-Host "   ${key}: $value" -ForegroundColor Gray
            }
        }
        
        if ($trip.resorts -and $trip.resorts.Count -gt 0) {
            Write-Host "`nResorts ($($trip.resorts.Count)):" -ForegroundColor Cyan
            foreach ($resort in $trip.resorts) {
                Write-Host "   - $resort" -ForegroundColor White
            }
        }
        
        if ($trip.notes) {
            Write-Host "`nNotes:" -ForegroundColor Cyan
            Write-Host "   $($trip.notes)" -ForegroundColor Gray
        }
        
        Write-Host ""
        return $trip
    }
    catch {
        Write-Host "Error fetching trip: $_" -ForegroundColor Red
        return $null
    }
}

function Compare-ResortsForTrip {
    param([string]$TripId)
    
    $trip = Get-TripDetails -TripId $TripId
    
    if (-not $trip) {
        return
    }
    
    if (-not $trip.resorts -or $trip.resorts.Count -eq 0) {
        Write-Host "No resorts configured for this trip." -ForegroundColor Yellow
        return
    }
    
    Write-Host "Running resort comparison..." -ForegroundColor Green
    
    $data = @{
        resorts = $trip.resorts
        departureDate = $trip.departureDate
        origin = "YOW"
        tripLength = $trip.tripLength
        budget = $trip.budget
        contingencyPercent = 15
        preferences = $trip.preferences
    }
    
    try {
        $response = Invoke-RestMethod -Uri "$apiUrl/api/resort-compare" -Method Post -ContentType "application/json" -Body ($data | ConvertTo-Json -Depth 10)
        
        Write-Host "`n=== TOP RESORT MATCHES ===" -ForegroundColor Cyan
        
        if ($trip.budget) {
            Write-Host "Budget: $([char]0x0024)$($trip.budget) | Within Budget: $($response.budgetInfo.withinBudgetCount)/$($response.budgetInfo.totalCount)" -ForegroundColor Yellow
        }
        
        Write-Host ""
        
        $topResorts = if ($response.comparisons.Count -gt 0) { $response.comparisons } else { $response.allComparisons }
        
        foreach ($comp in $topResorts | Select-Object -First 5) {
            $r = $comp.resort
            Write-Host "[$($comp.matchScore)] $($r.name)" -ForegroundColor White
            Write-Host "   Features: Food:$($r.features.food) Pool:$($r.features.pool) Beach:$($r.features.beach) Culture:$($r.features.culture)" -ForegroundColor Cyan
            Write-Host "   Package: $([char]0x0024)$([int]$comp.estimatedTotalMin)-$([char]0x0024)$([int]$comp.estimatedTotalMax) | $($r.priceRange) | Rating: $($r.tripAdvisorRating)" -ForegroundColor Gray
            
            if ($comp.budgetStatus) {
                $budgetColor = if ($comp.withinBudget) { "Green" } else { "Red" }
                Write-Host "   $($comp.budgetStatus)" -ForegroundColor $budgetColor
            }
            
            Write-Host ""
        }
        
        if ($response.packageSites) {
            Write-Host "Package Sites:" -ForegroundColor Cyan
            foreach ($site in $response.packageSites) {
                Write-Host "   - $site" -ForegroundColor Gray
            }
        }
    }
    catch {
        Write-Host "Error comparing resorts: $_" -ForegroundColor Red
    }
}

function Get-TripsByStatus {
    Write-Host "`nEnter status (planning/booked/completed): " -ForegroundColor Yellow -NoNewline
    $status = Read-Host
    
    try {
        $response = Invoke-RestMethod -Uri "$apiUrl/api/trips?status=$status" -Method Get
        
        Write-Host "`n=== TRIPS - $($status.ToUpper()) ($($response.count)) ===" -ForegroundColor Cyan
        Write-Host ""
        
        foreach ($trip in $response.trips) {
            Write-Host "$($trip.name)" -ForegroundColor White
            Write-Host "   $($trip.destination) | $($trip.region)" -ForegroundColor Gray
            if ($trip.departureDate) {
                Write-Host "   $($trip.departureDate)" -ForegroundColor Gray
            }
            Write-Host ""
        }
    }
    catch {
        Write-Host "Error fetching trips: $_" -ForegroundColor Red
    }
}

# Main loop
do {
    Show-Menu
    $choice = Read-Host "Select option"
    
    switch ($choice) {
        "1" {
            Get-AllTrips
        }
        "2" {
            Get-AllTrips
            Write-Host "`nEnter trip # (number): " -ForegroundColor Yellow -NoNewline
            $tripId = Read-Host
            Get-TripDetails -TripId $tripId
        }
        "3" {
            Get-AllTrips
            Write-Host "`nEnter trip # (number): " -ForegroundColor Yellow -NoNewline
            $tripId = Read-Host
            Compare-ResortsForTrip -TripId $tripId
        }
        "4" {
            Get-TripsByStatus
        }
        "5" {
            Write-Host "`nGoodbye!" -ForegroundColor Green
            break
        }
        default {
            Write-Host "Invalid option. Please try again." -ForegroundColor Red
        }
    }
    
    if ($choice -ne "5") {
        Write-Host "`nPress any key to continue..." -ForegroundColor DarkGray
        $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    
} while ($choice -ne "5")
