import { getServerApi } from "@garage/api/server"
import { VendorGeneralInfo } from "@/modules/vendors/vendor-general-info"

export default async function VendorDetailPage(props: { params: Promise<{ id: string }> }) {
    const { id } = await props.params
    const api = await getServerApi()
    // The OpenAPI schema doesn't yet describe GET /api/vendors/{id} (only PUT/DELETE),
    // so cast through `any` here until the schema is regenerated.
    // Guard the fetch: a failed request (404/401/5xx) must degrade gracefully
    // instead of throwing and white-screening the whole route.
    let vendor: Record<string, any> | undefined
    try {
        const response = (await api.vendors.getById(id)) as { data?: Record<string, any> }
        vendor = response?.data
    } catch (err) {
        console.error(`Failed to load vendor ${id}:`, err)
    }

    if (!vendor) {
        return <div className="text-muted-foreground p-6">Vendor not found.</div>
    }

    return <VendorGeneralInfo vendor={vendor} />
}
