/**
 * Rules shared by the freight and passenger support-chat attachment flows.
 *
 * The two chat backends are independent implementations (freight: TypeORM +
 * polymorphic `FileRecord`; passenger: Prisma + `SupportAttachment`), but the
 * *contract* a client codes against — what may be uploaded, how large, how the
 * preview URL behaves — must not drift between them. Keep the limits here so
 * both APIs validate identically and all four web apps can render one consistent
 * "file too large / type not allowed" message.
 */

/** `FileRecord.resource` discriminator for freight chat attachments. */
export const SUPPORT_ATTACHMENT_RESOURCE = "support_message";

/** Per-file ceiling. Enforced server-side; the UI pre-checks to fail fast. */
export const SUPPORT_ATTACHMENT_MAX_BYTES = 10 * 1024 * 1024;

/** Max files on a single message. */
export const SUPPORT_ATTACHMENT_MAX_PER_MESSAGE = 10;

/**
 * How long a minted preview URL stays valid. Long enough that an open thread
 * doesn't rot mid-read, short enough that a leaked URL isn't a durable grant.
 */
export const SUPPORT_ATTACHMENT_URL_TTL_SECONDS = 60 * 60;

/**
 * Types accepted on a chat message.
 *
 * Deliberately NARROWER than `FilesService.ALLOWED_UPLOAD_MIME` (which also
 * serves generated PDFs and scanned business documents at 25MB). Chat is
 * user-to-user, so the blast radius of a bad file is another human clicking it.
 *
 * `image/svg+xml` is excluded on purpose and must stay excluded: an SVG is
 * executable markup, and previewing one inline (`<img>` is safe, but an
 * `<iframe>`/direct navigation is not) executes any script it carries under the
 * serving origin. Nothing in chat needs vector uploads.
 */
export const SUPPORT_ATTACHMENT_ALLOWED_MIME: readonly string[] = [
  // images (previewable inline)
  "image/jpeg",
  "image/png",
  "image/webp",
  "image/gif",
  // documents
  "application/pdf",
  "application/msword",
  "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "application/vnd.ms-excel",
  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  "text/csv",
  "text/plain",
];

/** Image subset — these are the ones worth rendering as a thumbnail. */
export const SUPPORT_ATTACHMENT_IMAGE_MIME: readonly string[] = [
  "image/jpeg",
  "image/png",
  "image/webp",
  "image/gif",
];

export function isSupportAttachmentImage(mimeType: string): boolean {
  return SUPPORT_ATTACHMENT_IMAGE_MIME.includes(mimeType);
}

export function isSupportAttachmentAllowed(mimeType: string): boolean {
  return SUPPORT_ATTACHMENT_ALLOWED_MIME.includes(mimeType);
}

/** `accept` attribute for a file input / paste target. */
export const SUPPORT_ATTACHMENT_ACCEPT =
  SUPPORT_ATTACHMENT_ALLOWED_MIME.join(",");
