/**
 * File-upload types — mirror backend `src/modules/upload` exactly.
 *
 * Uploads are a two-phase, direct-to-storage flow:
 *   1. POST /uploads/presign  → { uploadUrl, s3Key: "tmp/…" }
 *   2. PUT the raw bytes straight to `uploadUrl` (S3/MinIO) — NOT through our
 *      axios instance. The presign signs `ContentType`, so the PUT MUST send the
 *      exact same `Content-Type`, or storage rejects it with SignatureDoesNotMatch.
 *
 * The returned tmp `s3Key` is later handed to an endpoint that commits it
 * (e.g. `POST /auth/provider/register` with `logoS3Key` / `documents[]`).
 *
 * Presign is `@Public()` for the provider logo/document types specifically, so
 * this works during registration before the user has an account.
 */

export enum UploadType {
  PROVIDER_LOGO = "provider-logo",
  PROVIDER_DOCUMENT = "provider-document",
  BOOKING_PROOF = "booking-proof",
}

export interface PresignUploadRequest {
  fileName: string;
  mimeType: string;
  /** Size in bytes */
  fileSize: number;
  uploadType: UploadType;
}

export interface PresignUploadResponse {
  /** Presigned S3 PUT URL, valid ~15 minutes */
  uploadUrl: string;
  /** Temporary key ("tmp/…") to hand to the committing endpoint */
  s3Key: string;
}

/** Why a file was rejected client-side (mapped to localized copy by the caller) */
export type UploadRejectionReason = "too-large" | "invalid-type";

/** A file the user has uploaded to temporary storage, tracked client-side */
export interface UploadedFileRef {
  /** tmp/ key to hand to the committing endpoint (register / commit) */
  s3Key: string;
  /** Original file name, for display in previews and summaries */
  fileName: string;
  /**
   * Object URL for previewing the file — an image thumbnail, or a link to open
   * a document in a new tab (client-only; revoked on replace/remove).
   */
  previewUrl?: string;
}

/**
 * Client-side mirror of backend `UPLOAD_RULES`, so a bad file gets an instant,
 * localized error instead of a wasted round-trip that returns 400.
 */
export const UPLOAD_RULES: Record<
  UploadType,
  { maxSize: number; allowedMimeTypes: string[] }
> = {
  [UploadType.PROVIDER_LOGO]: {
    maxSize: 5 * 1024 * 1024,
    allowedMimeTypes: ["image/jpeg", "image/png", "image/webp"],
  },
  [UploadType.PROVIDER_DOCUMENT]: {
    maxSize: 10 * 1024 * 1024,
    allowedMimeTypes: ["application/pdf", "image/jpeg", "image/png"],
  },
  [UploadType.BOOKING_PROOF]: {
    maxSize: 5 * 1024 * 1024,
    allowedMimeTypes: ["image/jpeg", "image/png", "image/webp"],
  },
};
