"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Label } from "@/components/ui/label" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { Loader2 } from "lucide-react" import { fetchProjects, fetchPackagesByProject, fetchLocationsByPackage, type Project, type Package as PackageType, type Location, type SessionContext } from "@/lib/api" type ProjectSelectionSectionProps = { onSelectionComplete: (session: SessionContext) => void } export function ProjectSelectionSection({ onSelectionComplete }: ProjectSelectionSectionProps) { // Data states const [projects, setProjects] = useState([]) const [packages, setPackages] = useState([]) const [locations, setLocations] = useState([]) // Selection states const [selectedProject, setSelectedProject] = useState(null) const [selectedPackage, setSelectedPackage] = useState(null) const [selectedLocation, setSelectedLocation] = useState(null) // Loading states const [loadingProjects, setLoadingProjects] = useState(true) const [loadingPackages, setLoadingPackages] = useState(false) const [loadingLocations, setLoadingLocations] = useState(false) // Error state const [error, setError] = useState(null) // Load projects on mount useEffect(() => { const loadProjects = async () => { try { setLoadingProjects(true) setError(null) const data = await fetchProjects() setProjects(data) } catch (err) { console.error("Failed to load projects:", err) setError("Failed to load projects. Please check if the backend is running.") } finally { setLoadingProjects(false) } } loadProjects() }, []) // Load packages when project changes useEffect(() => { if (!selectedProject) { setPackages([]) setSelectedPackage(null) return } const loadPackages = async () => { try { setLoadingPackages(true) setError(null) setSelectedPackage(null) setSelectedLocation(null) setLocations([]) const data = await fetchPackagesByProject(selectedProject.id) setPackages(data) } catch (err) { console.error("Failed to load packages:", err) setError("Failed to load packages for the selected project.") } finally { setLoadingPackages(false) } } loadPackages() }, [selectedProject]) // Load locations when package changes useEffect(() => { if (!selectedPackage) { setLocations([]) setSelectedLocation(null) return } const loadLocations = async () => { try { setLoadingLocations(true) setError(null) setSelectedLocation(null) const data = await fetchLocationsByPackage(selectedPackage.id) setLocations(data) } catch (err) { console.error("Failed to load locations:", err) setError("Failed to load locations for the selected package.") } finally { setLoadingLocations(false) } } loadLocations() }, [selectedPackage]) const handleProjectChange = (projectId: string) => { const project = projects.find(p => p.id === projectId) || null setSelectedProject(project) } const handlePackageChange = (packageId: string) => { const pkg = packages.find(p => p.id === packageId) || null setSelectedPackage(pkg) } const handleLocationChange = (locationId: string) => { const location = locations.find(l => l.id === locationId) || null setSelectedLocation(location) } const handleProceed = () => { if (selectedProject && selectedPackage && selectedLocation) { onSelectionComplete({ projectId: selectedProject.id, projectName: selectedProject.name, packageId: selectedPackage.id, packageName: selectedPackage.name, locationId: selectedLocation.id, locationName: selectedLocation.segment_name }) } } const isComplete = selectedProject && selectedPackage && selectedLocation // Step status helpers const getStepStatus = (step: number) => { if (step === 1) return selectedProject ? 'completed' : 'active' if (step === 2) return selectedPackage ? 'completed' : selectedProject ? 'active' : 'pending' if (step === 3) return selectedLocation ? 'completed' : selectedPackage ? 'active' : 'pending' return 'pending' } return (
Select Project Location Select Project, Package & Location to begin intelligent road analysis with advanced computer vision.
{/* Step Progress Indicator */}
{[1, 2, 3].map((step, index) => { const status = getStepStatus(step) const labels = ['Project', 'Package', 'Location'] return (
{step}
{labels[index]}
{index < 2 && (
)}
) })}
{/* Error Display */} {error && (
!

{error}

)}
{/* Project Dropdown */}
{/* Package Dropdown */}
{/* Location Dropdown */}
{/* Selected Summary */} {isComplete && (

Path: {selectedProject?.name} / {selectedPackage?.name} / {selectedLocation?.segment_name}

)} {/* Proceed Button */}
) }