import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import type { POStatus } from "@prisma/client";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

export function formatCurrency(amount: number | string, currency = "INR"): string {
  return new Intl.NumberFormat("en-IN", { style: "currency", currency }).format(
    Number(amount)
  );
}

export function formatDate(date: Date | string): string {
  return new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "short",
    day: "numeric",
  }).format(new Date(date));
}

export function formatDateTime(date: Date | string): string {
  return new Intl.DateTimeFormat("en-US", {
    year: "numeric",
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit",
  }).format(new Date(date));
}

export function generatePoNumber(): string {
  const year = new Date().getFullYear();
  const seq = Math.floor(Math.random() * 100000)
    .toString()
    .padStart(5, "0");
  return `PO-${year}-${seq}`;
}

export const PO_STATUS_LABELS: Record<POStatus, string> = {
  DRAFT: "Draft",
  SUBMITTED: "Submitted",
  MGR_REVIEW: "Under Review",
  VENDOR_ID_PENDING: "Vendor ID Pending",
  EDITS_REQUESTED: "Edits Requested",
  REJECTED: "Rejected",
  MGR_APPROVED: "Approved",
  SENT_FOR_PAYMENT: "Sent for Payment",
  PAID_DELIVERED: "Paid",
  CLOSED: "Closed",
};

export type BadgeVariant =
  | "default"
  | "secondary"
  | "success"
  | "warning"
  | "danger"
  | "outline";

export const PO_STATUS_VARIANTS: Record<POStatus, BadgeVariant> = {
  DRAFT: "outline",
  SUBMITTED: "secondary",
  MGR_REVIEW: "secondary",
  VENDOR_ID_PENDING: "warning",
  EDITS_REQUESTED: "warning",
  REJECTED: "danger",
  MGR_APPROVED: "success",
  SENT_FOR_PAYMENT: "default",
  PAID_DELIVERED: "success",
  CLOSED: "secondary",
};
