import type { ApiClientOptions } from "./infra/client"
import { AuthClient } from "./clients/auth"
import { CustomersClient } from "./clients/customers"
import { ReferralSourcesClient } from "./clients/referral-sources"
import { VehiclesClient } from "./clients/vehicles"
import { VehicleAttributesClient } from "./clients/vehicle-attributes"
import { VehicleDocumentsClient } from "./clients/vehicle-documents"
import { DepartmentsClient } from "./clients/departments"
import { EmployeesClient } from "./clients/employees"
import { GeoClient } from "./clients/geo"
import { PaymentTermsClient } from "./clients/payment-terms"
import { ShopTypesClient } from "./clients/shop-types"
import { InventoryClient } from "./clients/inventory"
import { VendorsClient } from "./clients/vendors"
import { InspectionsClient } from "./clients/inspections"
import { InspectionTemplatesClient } from "./clients/inspection-templates"
import { LabelsClient } from "./clients/labels"
import { InsuranceTypesClient } from "./clients/insurance-types"
import { EstimatesClient } from "./clients/estimates"
import { QuickRemarksClient } from "./clients/quick-remarks"
import { QuickNotesClient } from "./clients/quick-notes"
import { ShopRecommendationsClient } from "./clients/shop-recommendations"
import { JobCardsClient } from "./clients/job-cards"
import { PaymentModesClient } from "./clients/payment-modes"
import { PaymentReceivedClient } from "./clients/payment-received"
import { PartsClient } from "./clients/parts"
import { PurchaseOrdersClient } from "./clients/purchase-orders"
import { ServicesClient } from "./clients/services"
import { ServiceGroupsClient } from "./clients/service-groups"
import { ExpensesClient } from "./clients/expenses"
import { TasksClient } from "./clients/tasks"
import { TaskTypesClient } from "./clients/task-types"
import { TaskSectionsClient } from "./clients/task-sections"
import { AppointmentsClient } from "./clients/appointments"
import { ShopTimingsClient } from "./clients/shop-timings"
import { ShopCalendarsClient } from "./clients/shop-calendars"
import { HolidayYearsClient } from "./clients/holiday-years"
import { TaxesClient } from "./clients/taxes"
import { InvoicesClient } from "./clients/invoices"
import { HomeClient } from "./clients/home"
import { BillsClient } from "./clients/bills"
import { ReasonsClient } from "./clients/reasons"
import { HolidaysClient } from "./clients/holidays"
import { MakeAndModelsClient } from "./clients/make-and-models"
import { TimeSheetsClient } from "./clients/time-sheets"
import { InvoiceSequencesClient } from "./clients/invoice-sequences"
import { CreditNotesClient } from "./clients/credit-notes"
import { PaymentMadesClient } from "./clients/payment-mades"
import { VendorCreditsClient } from "./clients/vendor-credits"
import { InventoryAdjustmentsClient } from "./clients/inventory-adjustments"
import { ServiceGroupIncludesClient } from "./clients/service-group-includes"
import { ServiceGroupPricingsClient } from "./clients/service-group-pricings"
import { ServiceGroupServicesClient } from "./clients/service-group-services"
import { ServiceGroupPartsClient } from "./clients/service-group-parts"
import { SettingsClient } from "./clients/settings"
import { ConfigurationsClient } from "./clients/configurations"
import { AutoGenerateClient } from "./clients/auto-generate"
import { ExpenseItemsClient } from "./clients/expense-items"
import { InventoryCategoriesClient } from "./clients/inventory-categories"
import { DocumentPrintClient } from "./clients/document-print"
import { DocumentShareClient } from "./clients/document-share"

export type CreateApiOptions = ApiClientOptions & { baseUrl?: string }

/**
 * Default base URL used when no explicit override is passed. The dashboard
 * runs every browser call through the Next.js server proxy at /api/proxy so
 * the build-time NEXT_PUBLIC_API_URL never reaches the client.
 *
 * Server-side callers (server actions, RSC) can opt out by passing
 * `{ baseUrl: <absolute api URL> }` from a cookie.
 */
const DEFAULT_BASE_URL = "/api/proxy"

export function createApi(options?: CreateApiOptions) {
    const baseUrl = options?.baseUrl ?? DEFAULT_BASE_URL
    const restOptions: ApiClientOptions | undefined = options?.headers
        ? { headers: options.headers }
        : undefined

    return {
        auth: new AuthClient(baseUrl, restOptions),
        customers: new CustomersClient(baseUrl, restOptions),
        referralSources: new ReferralSourcesClient(baseUrl, restOptions),
        vehicles: new VehiclesClient(baseUrl, restOptions),
        vehicleAttributes: new VehicleAttributesClient(baseUrl, restOptions),
        vehicleDocuments: new VehicleDocumentsClient(baseUrl, restOptions),
        departments: new DepartmentsClient(baseUrl, restOptions),
        employees: new EmployeesClient(baseUrl, restOptions),
        geo: new GeoClient(baseUrl, restOptions),
        paymentTerms: new PaymentTermsClient(baseUrl, restOptions),
        shopTypes: new ShopTypesClient(baseUrl, restOptions),
        inventory: new InventoryClient(baseUrl, restOptions),
        vendors: new VendorsClient(baseUrl, restOptions),
        inspections: new InspectionsClient(baseUrl, restOptions),
        inspectionTemplates: new InspectionTemplatesClient(baseUrl, restOptions),
        labels: new LabelsClient(baseUrl, restOptions),
        insuranceTypes: new InsuranceTypesClient(baseUrl, restOptions),
        estimates: new EstimatesClient(baseUrl, restOptions),
        quickRemarks: new QuickRemarksClient(baseUrl, restOptions),
        quickNotes: new QuickNotesClient(baseUrl, restOptions),
        shopRecommendations: new ShopRecommendationsClient(baseUrl, restOptions),
        jobCards: new JobCardsClient(baseUrl, restOptions),
        paymentModes: new PaymentModesClient(baseUrl, restOptions),
        paymentReceived: new PaymentReceivedClient(baseUrl, restOptions),
        parts: new PartsClient(baseUrl, restOptions),
        purchaseOrders: new PurchaseOrdersClient(baseUrl, restOptions),
        services: new ServicesClient(baseUrl, restOptions),
        serviceGroups: new ServiceGroupsClient(baseUrl, restOptions),
        expenses: new ExpensesClient(baseUrl, restOptions),
        tasks: new TasksClient(baseUrl, restOptions),
        taskTypes: new TaskTypesClient(baseUrl, restOptions),
        taskSections: new TaskSectionsClient(baseUrl, restOptions),
        appointments: new AppointmentsClient(baseUrl, restOptions),
        shopTimings: new ShopTimingsClient(baseUrl, restOptions),
        shopCalendars: new ShopCalendarsClient(baseUrl, restOptions),
        holidayYears: new HolidayYearsClient(baseUrl, restOptions),
        taxes: new TaxesClient(baseUrl, restOptions),
        invoices: new InvoicesClient(baseUrl, restOptions),
        home: new HomeClient(baseUrl, restOptions),
        bills: new BillsClient(baseUrl, restOptions),
        reasons: new ReasonsClient(baseUrl, restOptions),
        holidays: new HolidaysClient(baseUrl, restOptions),
        makeAndModels: new MakeAndModelsClient(baseUrl, restOptions),
        timeSheets: new TimeSheetsClient(baseUrl, restOptions),
        invoiceSequences: new InvoiceSequencesClient(baseUrl, restOptions),
        creditNotes: new CreditNotesClient(baseUrl, restOptions),
        paymentMades: new PaymentMadesClient(baseUrl, restOptions),
        vendorCredits: new VendorCreditsClient(baseUrl, restOptions),
        inventoryAdjustments: new InventoryAdjustmentsClient(baseUrl, restOptions),
        serviceGroupIncludes: new ServiceGroupIncludesClient(baseUrl, restOptions),
        serviceGroupPricings: new ServiceGroupPricingsClient(baseUrl, restOptions),
        serviceGroupServices: new ServiceGroupServicesClient(baseUrl, restOptions),
        serviceGroupParts: new ServiceGroupPartsClient(baseUrl, restOptions),
        settings: new SettingsClient(baseUrl, restOptions),
        configurations: new ConfigurationsClient(baseUrl, restOptions),
        autoGenerate: new AutoGenerateClient(baseUrl, restOptions),
        expenseItems: new ExpenseItemsClient(baseUrl, restOptions),
        inventoryCategories: new InventoryCategoriesClient(baseUrl, restOptions),
        documentPrint: new DocumentPrintClient(baseUrl, restOptions),
        documentShare: new DocumentShareClient(baseUrl, restOptions),
    }
}

/** Unauthenticated singleton — use for public calls (login, register). */
export const api = createApi()
