import { TaxCatalogProvider } from '@erp/core';
import { TFunction } from 'i18next';
import { PropsWithChildren, createContext, useCallback, useContext, useMemo, useState } from "react";
import { useTranslation } from "../i18n";
import { MODULE_NAME } from '../manifest';

export type InvoiceContextValue = {
  company_id: string;
  invoice_id: string;
  status: string;
  currency_code: string;
  language_code: string;
  is_proforma: boolean;

  readOnly: boolean;
  taxCatalog: TaxCatalogProvider;

  t: TFunction<typeof MODULE_NAME>;

  changeLanguage: (lang: string) => void;
  changeCurrency: (currency: string) => void;
  changeIsProforma: (value: boolean) => void;
};

const InvoiceContext = createContext<InvoiceContextValue | null>(null);

export interface InvoiceProviderParams {
  taxCatalog: TaxCatalogProvider;
  invoice_id: string;
  company_id: string;
  status: string; // default "draft"
  language_code?: string; // default "es"
  currency_code?: string; // default "EUR"
  is_proforma?: boolean; // default 'true'
  readOnly?: boolean; // default 'false'

  children: React.ReactNode;
}

export const InvoiceProvider = ({ taxCatalog: initialTaxCatalog, invoice_id, company_id, status: initialStatus = "draft", language_code: initialLang = "es",
  currency_code: initialCurrency = "EUR", readOnly: initialReadOnly = false,
  is_proforma: initialProforma = true, children }: PropsWithChildren<InvoiceProviderParams>) => {

  const { t } = useTranslation();

  // Estado interno local para campos dinámicos
  const [language_code, setLanguage] = useState(initialLang);
  const [currency_code, setCurrency] = useState(initialCurrency);
  const [is_proforma, setIsProforma] = useState(initialProforma);
  const [readOnly, setReadOnly] = useState(initialReadOnly);
  const [status] = useState(initialStatus);
  const [taxCatalog] = useState(initialTaxCatalog);

  // Callbacks memoizados
  const setLanguageMemo = useCallback((language_code: string) => setLanguage(language_code), []);
  const setCurrencyMemo = useCallback((currency_code: string) => setCurrency(currency_code), []);
  const setIsProformaMemo = useCallback((is_proforma: boolean) => setIsProforma(is_proforma), []);
  const setReadOnlyMemo = useCallback((readOnly: boolean) => setReadOnly(readOnly), []);

  const value = useMemo<InvoiceContextValue>(() => {

    return {
      t,

      invoice_id,
      company_id,
      status,
      language_code,
      currency_code,
      is_proforma,

      taxCatalog,
      readOnly,

      changeLanguage: setLanguageMemo,
      changeCurrency: setCurrencyMemo,
      changeIsProforma: setIsProformaMemo,
      setReadOnly: setReadOnlyMemo,
    }
  }, [t, readOnly, company_id, invoice_id, status, language_code, currency_code, is_proforma, taxCatalog, setLanguageMemo, setCurrencyMemo, setIsProformaMemo, setReadOnlyMemo]);

  return <InvoiceContext.Provider value={value}>{children}</InvoiceContext.Provider>;
};


export function useInvoiceContext(): InvoiceContextValue {
  const context = useContext(InvoiceContext);
  if (!context) {
    throw new Error("useInvoiceContext must be used within <InvoiceProvider>");
  }
  return context;
}