export const ACCOUNT_TYPES = ["RETAIL", "SEMI_WHOLESALE", "WHOLESALE"] as const;
export const REGIONS = ["Algiers", "Oran", "Constantine", "Blida", "Setif"] as const;
export const DEPARTMENTS = [
  "users",
  "activity",
  "customers",
  "products",
  "purchases",
  "store",
  "pricing",
  "orders",
  "invoices",
  "settings",
  "rounds",
  "fleet",
  "payments",
  "statistics",
  "analysis",
  "notifications",
] as const;

export type Department = (typeof DEPARTMENTS)[number];
export type Operation = "view" | "create" | "edit" | "delete";

export function money(value: number) {
  return new Intl.NumberFormat("en-DZ", {
    style: "currency",
    currency: "DZD",
    maximumFractionDigits: 0,
  }).format(value);
}

export function label(value: string) {
  return value.replaceAll("_", " ").toLowerCase().replace(/^\w/, (c) => c.toUpperCase());
}

export function csvEscape(value: string | number | null | undefined) {
  const text = value == null ? "" : String(value);
  if (/[",\n]/.test(text)) return `"${text.replaceAll('"', '""')}"`;
  return text;
}

export function toCsv(headers: string[], rows: Array<Array<string | number | null | undefined>>) {
  const lines = [headers.map(csvEscape).join(","), ...rows.map((row) => row.map(csvEscape).join(","))];
  return `\uFEFF${lines.join("\n")}`;
}

const ONES = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
const TEENS = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"];
const TENS = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"];

function chunkToWords(n: number) {
  const hundred = Math.floor(n / 100);
  const rest = n % 100;
  const parts: string[] = [];
  if (hundred) parts.push(`${ONES[hundred]} hundred`);
  if (rest >= 10 && rest < 20) parts.push(TEENS[rest - 10]);
  else {
    const ten = Math.floor(rest / 10);
    const one = rest % 10;
    if (ten) parts.push(TENS[ten]);
    if (one) parts.push(ONES[one]);
  }
  return parts.join(" ");
}

export function amountInWords(value: number) {
  const n = Math.round(Math.abs(value));
  if (n === 0) return "Zero dinars";
  const scales = [
    [1_000_000_000, "billion"],
    [1_000_000, "million"],
    [1_000, "thousand"],
  ] as const;
  let remaining = n;
  const parts: string[] = [];
  for (const [size, name] of scales) {
    const count = Math.floor(remaining / size);
    if (count) {
      parts.push(`${chunkToWords(count)} ${name}`);
      remaining %= size;
    }
  }
  if (remaining) parts.push(chunkToWords(remaining));
  return `${parts.join(" ")} dinars`.replace(/\s+/g, " ").replace(/^./, (c) => c.toUpperCase());
}

export function toneForStatus(status: string): "neutral" | "good" | "warn" | "bad" | "accent" {
  const s = status.toUpperCase();
  // Stock movement types: entries, exits, losses, corrections.
  if (["PURCHASE", "OPENING", "TRUCK_RETURN", "SALE_CANCEL", "CUSTOMER_RETURN", "RECEIVED", "STORE_RETURN", "COMPLETED"].includes(s)) return "good";
  if (["SALE", "TRUCK_LOAD", "STORE_TRANSFER"].includes(s)) return "accent";
  if (["LOSS", "PURCHASE_CANCEL"].includes(s)) return "bad";
  if (["ADJUSTMENT", "INVENTORY"].includes(s)) return "warn";
  if (["ACTIVE", "DELIVERED", "VALIDATED", "PAID", "RECONCILED", "CLOSED", "GOOD", "FULL_PAID", "DELIVERY"].includes(s)) return "good";
  if (["PENDING", "NEW", "DRAFT", "PLANNED", "SCHEDULED", "IN_PROGRESS", "PARTIAL", "PART_PAID", "PROSPECTING"].includes(s)) return "warn";
  if (["REJECTED", "CANCELLED", "INACTIVE", "FAILED", "OVERDUE"].includes(s)) return "bad";
  return "accent";
}
