import { DashboardDetailsPage } from '@/base/components/layout/dashboard'
import { getServerApi } from '@garage/api/server'
import { EstimateActions } from '@/modules/estimates/estimate-actions'
import { EstimateProvider } from '@/modules/estimates/estimate-context'
import { FileTextIcon } from 'lucide-react'
import React from 'react'

export default async function layout(props: {
    params: Promise<{ id: string }>
    children: React.ReactNode
}) {
    const { id } = await props.params
    const api = await getServerApi()
    const estimate = await api.estimates.getById(id)

    const estimateData = (estimate as any)?.data
    const title = estimateData?.title || estimateData?.estimate_number || `Estimate #${id}`
    const estimateLabel = estimateData?.estimate_number
        ? `${estimateData.estimate_number}${estimateData.title ? ` — ${estimateData.title}` : ''}`
        : title

    return (
        <EstimateProvider estimate={{ id, label: estimateLabel }}>
            <DashboardDetailsPage
                className="p-0 lg:p-0"
                icon={<FileTextIcon className="size-5" />}
                title={title}
                description={
                    estimateData?.date ? `Date: ${estimateData.date}` : undefined
                }
                backHref="/sales/estimates"
                actions={<EstimateActions estimateId={id} />}
                tabs={[
                    { href: `/sales/estimates/${id}`, label: 'Details' },
                ]}
            >
                {props.children}
            </DashboardDetailsPage>
        </EstimateProvider>
    )
}
