/**
 * Phân loại văn bản công ty — theo yêu cầu "văn bản, thông báo, quy định, tài liệu…".
 * Không import gì từ tầng cơ sở dữ liệu nên dùng được ở cả máy chủ lẫn trình duyệt.
 * Lưu khoá chuỗi (không dùng enum Postgres) để thêm loại mới không cần migration.
 */
export const DOCUMENT_CATEGORIES = [
  { key: "VAN_BAN", label: "Văn bản", hint: "Công văn, quyết định, hợp đồng mẫu" },
  { key: "THONG_BAO", label: "Thông báo", hint: "Thông báo chung cần lưu lại lâu dài" },
  { key: "QUY_DINH", label: "Quy định – Quy chế", hint: "Nội quy, quy chế, chính sách" },
  { key: "TAI_LIEU", label: "Tài liệu", hint: "Tài liệu sản phẩm, đào tạo, hướng dẫn" },
  { key: "KHAC", label: "Khác", hint: "" },
] as const;

export type DocumentCategoryKey = (typeof DOCUMENT_CATEGORIES)[number]["key"];

export const DEFAULT_DOCUMENT_CATEGORY: DocumentCategoryKey = "VAN_BAN";

export const DOCUMENT_CATEGORY_LABEL: Record<string, string> = Object.fromEntries(DOCUMENT_CATEGORIES.map((c) => [c.key, c.label]));

export function isDocumentCategory(value: unknown): value is DocumentCategoryKey {
  return DOCUMENT_CATEGORIES.some((c) => c.key === value);
}

/** Tệp được nhận cho văn bản công ty (khai báo ở trình duyệt; máy chủ vẫn kiểm lại nội dung thật của tệp). */
export const DOCUMENT_FILE_ACCEPT =
  ".pdf,.png,.jpg,.jpeg,.docx,.xlsx,.pptx,application/pdf,image/png,image/jpeg,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.presentationml.presentation";

export const DOCUMENT_ALLOWED_MIME = [
  "application/pdf",
  "image/png",
  "image/jpeg",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "application/vnd.openxmlformats-officedocument.presentationml.presentation",
] as const;

export const DOCUMENT_MAX_MB = 8;

export function formatFileSize(bytes: number): string {
  if (bytes < 1024) return `${bytes} B`;
  if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(0)} KB`;
  return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}

/** Nhãn ngắn của loại tệp để hiện cạnh tên (PDF, Word, Excel…). */
export function fileKindLabel(mimeType: string): string {
  if (mimeType === "application/pdf") return "PDF";
  if (mimeType.startsWith("image/")) return "Ảnh";
  if (mimeType.includes("wordprocessingml")) return "Word";
  if (mimeType.includes("spreadsheetml")) return "Excel";
  if (mimeType.includes("presentationml")) return "PowerPoint";
  return "Tệp";
}

const MIME_BY_EXTENSION: Record<string, string> = {
  pdf: "application/pdf",
  png: "image/png",
  jpg: "image/jpeg",
  jpeg: "image/jpeg",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
};

/**
 * Loại tệp do trình duyệt khai báo; máy Windows không cài Office thường gửi rỗng hoặc `application/octet-stream`
 * cho .docx/.xlsx/.pptx — khi đó lấy theo đuôi tệp (nội dung thật vẫn được máy chủ kiểm lại).
 */
export function resolveDocumentMime(fileName: string, declared: string): string {
  if (declared && declared !== "application/octet-stream") return declared;
  return MIME_BY_EXTENSION[fileName.split(".").pop()?.toLowerCase() ?? ""] ?? declared;
}
