import type { DocumentType } from "@/types";

export interface Market {
  countryCode: string;
  countryName: string;
  region: "Sudamérica" | "Centroamérica y México" | "Caribe" | "Hispanohablantes fuera de América";
  locale: string;
  currencyCode: string;
  currencyName: string;
  fiscalIdLabel: string;
  sampleFiscalId: string;
  sampleAddress: string;
  taxName: string;
  taxRate: number;
  secondaryCurrencyCodes?: string[];
  note?: string;
}

export const DEFAULT_MARKET_CODE = "PE";
export const DEFAULT_BUSINESS_NAME = "Tienda Demo";
export const DEFAULT_TAX_RATE = 0.18;

// Fallback comprobantes when a store has none configured yet.
export const DEFAULT_DOCUMENT_TYPES: DocumentType[] = [
  { name: "Nota de venta", chargesIva: false },
  { name: "Boleta", chargesIva: true },
  { name: "Factura", chargesIva: true },
];

export const SUPPORTED_MARKETS: Market[] = [
  {
    countryCode: "AR",
    countryName: "Argentina",
    region: "Sudamérica",
    locale: "es-AR",
    currencyCode: "ARS",
    currencyName: "Peso argentino",
    fiscalIdLabel: "CUIT/CUIL",
    sampleFiscalId: "30-00000000-0",
    sampleAddress: "Av. Corrientes 123, Buenos Aires",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "BO",
    countryName: "Bolivia",
    region: "Sudamérica",
    locale: "es-BO",
    currencyCode: "BOB",
    currencyName: "Boliviano",
    fiscalIdLabel: "NIT",
    sampleFiscalId: "1000000001",
    sampleAddress: "Av. Arce 123, La Paz",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "BR",
    countryName: "Brasil",
    region: "Sudamérica",
    locale: "pt-BR",
    currencyCode: "BRL",
    currencyName: "Real brasileño",
    fiscalIdLabel: "CNPJ",
    sampleFiscalId: "00.000.000/0001-00",
    sampleAddress: "Av. Paulista 123, São Paulo",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "CL",
    countryName: "Chile",
    region: "Sudamérica",
    locale: "es-CL",
    currencyCode: "CLP",
    currencyName: "Peso chileno",
    fiscalIdLabel: "RUT",
    sampleFiscalId: "76.000.000-0",
    sampleAddress: "Av. Providencia 123, Santiago",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "CO",
    countryName: "Colombia",
    region: "Sudamérica",
    locale: "es-CO",
    currencyCode: "COP",
    currencyName: "Peso colombiano",
    fiscalIdLabel: "NIT",
    sampleFiscalId: "900000000-1",
    sampleAddress: "Carrera 7 # 123, Bogotá",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "EC",
    countryName: "Ecuador",
    region: "Sudamérica",
    locale: "es-EC",
    currencyCode: "USD",
    currencyName: "Dólar estadounidense",
    fiscalIdLabel: "RUC",
    sampleFiscalId: "1790000000001",
    sampleAddress: "Av. Amazonas 123, Quito",
    taxName: "IVA",
    taxRate: 0.15,
  },
  {
    countryCode: "GY",
    countryName: "Guyana",
    region: "Sudamérica",
    locale: "es-GY",
    currencyCode: "GYD",
    currencyName: "Dólar guyanés",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Regent Street 123, Georgetown",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "PY",
    countryName: "Paraguay",
    region: "Sudamérica",
    locale: "es-PY",
    currencyCode: "PYG",
    currencyName: "Guaraní paraguayo",
    fiscalIdLabel: "RUC",
    sampleFiscalId: "80000000-0",
    sampleAddress: "Av. España 123, Asunción",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "PE",
    countryName: "Perú",
    region: "Sudamérica",
    locale: "es-PE",
    currencyCode: "PEN",
    currencyName: "Sol peruano",
    fiscalIdLabel: "RUC",
    sampleFiscalId: "20100000001",
    sampleAddress: "Av. Demo 123, Lima",
    taxName: "IGV demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "SR",
    countryName: "Surinam",
    region: "Sudamérica",
    locale: "es-SR",
    currencyCode: "SRD",
    currencyName: "Dólar surinamés",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Kwattaweg 123, Paramaribo",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "UY",
    countryName: "Uruguay",
    region: "Sudamérica",
    locale: "es-UY",
    currencyCode: "UYU",
    currencyName: "Peso uruguayo",
    fiscalIdLabel: "RUT",
    sampleFiscalId: "210000000018",
    sampleAddress: "Av. 18 de Julio 123, Montevideo",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "VE",
    countryName: "Venezuela",
    region: "Sudamérica",
    locale: "es-VE",
    currencyCode: "VES",
    currencyName: "Bolívar soberano",
    fiscalIdLabel: "RIF",
    sampleFiscalId: "J-00000000-0",
    sampleAddress: "Av. Libertador 123, Caracas",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
    note: "Se usa VES como código operativo; VED aparece en algunos listados ISO/CLDR para redenominación.",
  },
  {
    countryCode: "BZ",
    countryName: "Belice",
    region: "Centroamérica y México",
    locale: "es-BZ",
    currencyCode: "BZD",
    currencyName: "Dólar beliceño",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Albert Street 123, Belize City",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "CR",
    countryName: "Costa Rica",
    region: "Centroamérica y México",
    locale: "es-CR",
    currencyCode: "CRC",
    currencyName: "Colón costarricense",
    fiscalIdLabel: "Cédula jurídica",
    sampleFiscalId: "3-101-000000",
    sampleAddress: "Av. Central 123, San José",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "SV",
    countryName: "El Salvador",
    region: "Centroamérica y México",
    locale: "es-SV",
    currencyCode: "USD",
    currencyName: "Dólar estadounidense",
    fiscalIdLabel: "NIT",
    sampleFiscalId: "0614-010101-101-0",
    sampleAddress: "Alameda Roosevelt 123, San Salvador",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "GT",
    countryName: "Guatemala",
    region: "Centroamérica y México",
    locale: "es-GT",
    currencyCode: "GTQ",
    currencyName: "Quetzal guatemalteco",
    fiscalIdLabel: "NIT",
    sampleFiscalId: "1000000-1",
    sampleAddress: "Zona 10, Ciudad de Guatemala",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "HN",
    countryName: "Honduras",
    region: "Centroamérica y México",
    locale: "es-HN",
    currencyCode: "HNL",
    currencyName: "Lempira hondureño",
    fiscalIdLabel: "RTN",
    sampleFiscalId: "08011990123456",
    sampleAddress: "Boulevard Morazán 123, Tegucigalpa",
    taxName: "ISV demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "MX",
    countryName: "México",
    region: "Centroamérica y México",
    locale: "es-MX",
    currencyCode: "MXN",
    currencyName: "Peso mexicano",
    fiscalIdLabel: "RFC",
    sampleFiscalId: "XAXX010101000",
    sampleAddress: "Av. Reforma 123, Ciudad de México",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "NI",
    countryName: "Nicaragua",
    region: "Centroamérica y México",
    locale: "es-NI",
    currencyCode: "NIO",
    currencyName: "Córdoba nicaragüense",
    fiscalIdLabel: "RUC",
    sampleFiscalId: "J0310000000000",
    sampleAddress: "Carretera Masaya 123, Managua",
    taxName: "IVA demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "PA",
    countryName: "Panamá",
    region: "Centroamérica y México",
    locale: "es-PA",
    currencyCode: "PAB",
    currencyName: "Balboa panameño",
    fiscalIdLabel: "RUC",
    sampleFiscalId: "155000000-2-2025",
    sampleAddress: "Vía España 123, Ciudad de Panamá",
    taxName: "ITBMS demo",
    taxRate: DEFAULT_TAX_RATE,
    secondaryCurrencyCodes: ["USD"],
    note: "Panamá usa balboa y dólar estadounidense; el balboa queda como moneda local principal.",
  },
  {
    countryCode: "AG",
    countryName: "Antigua y Barbuda",
    region: "Caribe",
    locale: "es-AG",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Market Street 123, Saint John's",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "BS",
    countryName: "Bahamas",
    region: "Caribe",
    locale: "es-BS",
    currencyCode: "BSD",
    currencyName: "Dólar bahameño",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Bay Street 123, Nassau",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "BB",
    countryName: "Barbados",
    region: "Caribe",
    locale: "es-BB",
    currencyCode: "BBD",
    currencyName: "Dólar de Barbados",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Broad Street 123, Bridgetown",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "CU",
    countryName: "Cuba",
    region: "Caribe",
    locale: "es-CU",
    currencyCode: "CUP",
    currencyName: "Peso cubano",
    fiscalIdLabel: "NIT",
    sampleFiscalId: "01000000000",
    sampleAddress: "Calle Obispo 123, La Habana",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "DM",
    countryName: "Dominica",
    region: "Caribe",
    locale: "es-DM",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "King George V Street 123, Roseau",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "GD",
    countryName: "Granada",
    region: "Caribe",
    locale: "es-GD",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Grand Anse 123, Saint George's",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "HT",
    countryName: "Haití",
    region: "Caribe",
    locale: "es-HT",
    currencyCode: "HTG",
    currencyName: "Gourde haitiano",
    fiscalIdLabel: "NIF",
    sampleFiscalId: "100000000",
    sampleAddress: "Rue Capois 123, Puerto Príncipe",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
    secondaryCurrencyCodes: ["USD"],
  },
  {
    countryCode: "JM",
    countryName: "Jamaica",
    region: "Caribe",
    locale: "es-JM",
    currencyCode: "JMD",
    currencyName: "Dólar jamaicano",
    fiscalIdLabel: "TRN",
    sampleFiscalId: "100-000-000",
    sampleAddress: "Hope Road 123, Kingston",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "PR",
    countryName: "Puerto Rico",
    region: "Caribe",
    locale: "es-PR",
    currencyCode: "USD",
    currencyName: "Dólar estadounidense",
    fiscalIdLabel: "EIN/SSN",
    sampleFiscalId: "66-0000000",
    sampleAddress: "Av. Ponce de León 123, San Juan",
    taxName: "IVU demo",
    taxRate: DEFAULT_TAX_RATE,
    note: "Territorio incluido por cobertura hispanohablante en América.",
  },
  {
    countryCode: "DO",
    countryName: "República Dominicana",
    region: "Caribe",
    locale: "es-DO",
    currencyCode: "DOP",
    currencyName: "Peso dominicano",
    fiscalIdLabel: "RNC",
    sampleFiscalId: "100000000",
    sampleAddress: "Av. Winston Churchill 123, Santo Domingo",
    taxName: "ITBIS demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "KN",
    countryName: "San Cristóbal y Nieves",
    region: "Caribe",
    locale: "es-KN",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Bay Road 123, Basseterre",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "VC",
    countryName: "San Vicente y las Granadinas",
    region: "Caribe",
    locale: "es-VC",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Bay Street 123, Kingstown",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "LC",
    countryName: "Santa Lucía",
    region: "Caribe",
    locale: "es-LC",
    currencyCode: "XCD",
    currencyName: "Dólar del Caribe Oriental",
    fiscalIdLabel: "Taxpayer ID",
    sampleFiscalId: "100000000",
    sampleAddress: "Jeremie Street 123, Castries",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "TT",
    countryName: "Trinidad y Tobago",
    region: "Caribe",
    locale: "es-TT",
    currencyCode: "TTD",
    currencyName: "Dólar de Trinidad y Tobago",
    fiscalIdLabel: "BIR number",
    sampleFiscalId: "100000000",
    sampleAddress: "Independence Square 123, Puerto España",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
  },
  {
    countryCode: "GQ",
    countryName: "Guinea Ecuatorial",
    region: "Hispanohablantes fuera de América",
    locale: "es-GQ",
    currencyCode: "XAF",
    currencyName: "Franco CFA de África Central",
    fiscalIdLabel: "NIF",
    sampleFiscalId: "P0000000",
    sampleAddress: "Av. de la Independencia 123, Malabo",
    taxName: "Impuesto demo",
    taxRate: DEFAULT_TAX_RATE,
    note: "País hispanohablante incluido; España queda excluida por requerimiento.",
  },
];

export const MARKET_REGIONS: Market["region"][] = [
  "Sudamérica",
  "Centroamérica y México",
  "Caribe",
  "Hispanohablantes fuera de América",
];

export function getMarketByCountryCode(countryCode?: string | null) {
  return (
    SUPPORTED_MARKETS.find((market) => market.countryCode === countryCode) ??
    SUPPORTED_MARKETS.find((market) => market.countryCode === DEFAULT_MARKET_CODE)!
  );
}

export function formatCurrencyAmount(
  value: number,
  market: Pick<Market, "locale" | "currencyCode"> = getMarketByCountryCode(DEFAULT_MARKET_CODE),
) {
  try {
    return new Intl.NumberFormat(market.locale, {
      style: "currency",
      currency: market.currencyCode,
      currencyDisplay: "narrowSymbol",
    }).format(value);
  } catch {
    return `${market.currencyCode} ${value.toFixed(2)}`;
  }
}

export function getCurrencyLabel(market: Market) {
  const secondary = market.secondaryCurrencyCodes?.length
    ? ` / ${market.secondaryCurrencyCodes.join(", ")}`
    : "";

  return `${market.currencyName} (${market.currencyCode}${secondary})`;
}
