"use client"

import { useFormContext } from "react-hook-form"

import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card"
import {
    useDocumentTotals,
    type DocumentLineItem,
} from "@/shared/hooks/use-document-totals"
import { DocumentTotalsSummary } from "@/shared/components/document-totals-summary"

import type { BillFormValues } from "./bill.schema"

function parseTaxRateFromLabel(label: string | undefined | null): number | undefined {
    if (!label) return undefined
    const match = label.match(/\((\d+(?:\.\d+)?)%\)/)
    return match ? Number(match[1]) : undefined
}

export function BillFormSummary() {
    const { watch } = useFormContext<BillFormValues>()

    const partItems = watch("part_items") ?? []
    const serviceItems = watch("service_items") ?? []
    const expenseItems = watch("expense_items") ?? []
    const discountType = watch("discount")
    const discountAmount = watch("discount_amount")
    const taxRelation = watch("tax")

    const taxRate = parseTaxRateFromLabel(taxRelation?.label)
    const taxLabel = taxRelation?.label ?? "Tax"

    const lineItems: DocumentLineItem[] = [
        ...partItems.map((p) => ({
            quantity: p.quantity,
            rate: p.rate,
            discount_amount: p.discount_amount,
        })),
        ...serviceItems.map((s) => ({
            quantity: s.quantity,
            rate: s.rate,
            discount_amount: s.discount_amount,
        })),
        ...expenseItems.map((e) => ({
            quantity: e.quantity,
            rate: e.rate,
            discount_amount: e.discount_amount,
        })),
    ]

    const groupLabels: Record<string, number> = {}
    if (partItems.length > 0) {
        groupLabels[`Parts (${partItems.length})`] = partItems.reduce(
            (s, p) => s + (Number(p.quantity) || 0) * (Number(p.rate) || 0),
            0,
        )
    }
    if (serviceItems.length > 0) {
        groupLabels[`Services (${serviceItems.length})`] = serviceItems.reduce(
            (s, sv) => s + (Number(sv.quantity) || 0) * (Number(sv.rate) || 0),
            0,
        )
    }
    if (expenseItems.length > 0) {
        groupLabels[`Expenses (${expenseItems.length})`] = expenseItems.reduce(
            (s, e) => s + (Number(e.quantity) || 0) * (Number(e.rate) || 0),
            0,
        )
    }

    const totals = useDocumentTotals({
        lineItems,
        discountType,
        discountAmount,
        taxRate,
    })

    if (!totals.hasLineItems) return null

    return (
        <Card>
            <CardHeader className="pb-3">
                <CardTitle className="text-sm font-medium text-muted-foreground uppercase tracking-wide">
                    Summary
                </CardTitle>
            </CardHeader>
            <CardContent>
                <DocumentTotalsSummary
                    totals={totals}
                    discountType={discountType}
                    taxLabel={taxLabel}
                    groupLabels={Object.keys(groupLabels).length > 1 ? groupLabels : undefined}
                />
            </CardContent>
        </Card>
    )
}
