"use client"

import { createContext, useContext } from "react"

export type BillVendor = {
    id?: number | null
    first_name?: string | null
    last_name?: string | null
    company_name?: string | null
    name?: string | null
}

export type BillResponse = {
    id?: number | null
    title?: string | null
    job_card_id?: number | null
    vendor_id?: number | null
    vendor_address_id?: number | null
    purchase_order_id?: number | null
    bill_number?: string | null
    bill_date?: string | null
    bill_due_date?: string | null
    payment_terms_id?: number | null
    department_id?: number | null
    notes?: string | null
    status?: string | null
    discount_type?: string | null
    tax_id?: number | null
    sub_total?: number | null
    tax_amount?: number | null
    total?: number | null
    payments_made?: number | null
    balance_due?: number | null
    discount_amount_major?: number | null
    created_at?: string | null
    updated_at?: string | null
    vendor?: BillVendor | null
    vendor_address?: {
        id?: number | null
        address?: string | null
        country?: { id?: number; name?: string } | null
        state?: { id?: number; name?: string } | null
    } | null
    department?: { id?: number | null; name?: string | null } | null
    job_card?: { id?: number | null; order_number?: string | null; estimate_number?: string | null } | null
    purchase_order?: { id?: number | null; order_number?: string | null } | null
    tax?: { id?: number | null; name?: string | null; rate?: string | null } | null
    payment_terms?: { id?: number | null; name?: string | null } | null
    labels?: { id?: number; title?: string; color_code?: string }[]
    parts?: any[]
    services?: any[]
    expenses?: any[]
}

type BillContextValue = BillResponse

const BillContext = createContext<BillContextValue | null>(null)

export function BillProvider({
    bill,
    children,
}: {
    bill: BillContextValue
    children: React.ReactNode
}) {
    return (
        <BillContext.Provider value={bill}>
            {children}
        </BillContext.Provider>
    )
}

export function useBill() {
    return useContext(BillContext)
}
