import { BaseEntity } from "@edr/api-common";
import { Column, Entity } from "typeorm";

/**
 * Reviewer verdict on a single stored document.
 *
 * `null` (the default) means "not reviewed" — the state every file starts in and
 * the only state the customer is not blocked by. `change_requested` is raised by
 * a backoffice reviewer against one specific document and is what the customer
 * must clear by re-uploading; `approved` records an explicit sign-off.
 */
export type FileReviewStatus = "change_requested" | "approved";

@Entity({ schema: "freight", name: "files" })
export class FileRecord extends BaseEntity {
  @Column({ name: "resource_id", type: "uuid" })
  resourceId!: string;

  @Column({ name: "resource", type: "varchar", length: 100 })
  resource!: string;

  @Column({ name: "code", type: "varchar", length: 100 })
  code!: string;

  @Column({ name: "name", type: "varchar", length: 500 })
  name!: string;

  @Column({ name: "url", type: "text" })
  url!: string;

  @Column({ name: "size", type: "integer" })
  size!: number;

  @Column({ name: "mime_type", type: "varchar", length: 255 })
  mimeType!: string;

  /** Reviewer verdict, or `null` while the document has never been reviewed. */
  @Column({
    name: "review_status",
    type: "varchar",
    length: 32,
    nullable: true,
    default: null,
  })
  reviewStatus!: FileReviewStatus | null;

  /** Why a change was requested — shown verbatim to the customer. */
  @Column({ name: "review_note", type: "text", nullable: true })
  reviewNote!: string | null;

  @Column({ name: "reviewed_by", type: "uuid", nullable: true })
  reviewedBy!: string | null;

  @Column({ name: "reviewed_at", type: "timestamptz", nullable: true })
  reviewedAt!: Date | null;

  /**
   * Who replaced this version, when a newer file took its place. Superseded
   * versions are soft-deleted rather than dropped, so the original a customer
   * uploaded survives a staff correction and the two can be compared.
   */
  @Column({ name: "replaced_by_user_id", type: "uuid", nullable: true })
  replacedByUserId!: string | null;

  /** Why the file was replaced — shown on the document's version history. */
  @Column({ name: "replace_reason", type: "text", nullable: true })
  replaceReason!: string | null;
}
