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 { CreateInvoiceFromEstimateButton } from '@/modules/estimates/create-invoice-from-estimate-button'
import { CreateJobCardFromEstimateButton } from '@/modules/estimates/create-job-card-from-estimate-button'
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.show(id)

    const estimateData = estimate?.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, data: estimateData }}>
            <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={
                    <div className="flex items-center gap-2">
                        <CreateInvoiceFromEstimateButton />
                        <CreateJobCardFromEstimateButton />
                        <EstimateActions estimateId={id} />
                    </div>
                }
                tabs={[
                    { href: `/sales/estimates/${id}`, label: 'Details' },
                ]}
            >
                {props.children}
            </DashboardDetailsPage>
        </EstimateProvider>
    )
}
