import { BaseEntity } from '@edr/api-common';
import { Column, Entity, Index, JoinColumn, ManyToOne } from 'typeorm';
import { Contract } from './contract.entity';

export const CONTRACT_DOC_REVIEW_STATUSES = ['PENDING', 'APPROVED', 'QUERIED'] as const;
export type ContractDocReviewStatus =
  (typeof CONTRACT_DOC_REVIEW_STATUSES)[number];

export const CONTRACT_DOC_UPLOADER_ROLES = ['CUSTOMER', 'GL_ET', 'GL_DJ'] as const;
export type ContractDocUploaderRole =
  (typeof CONTRACT_DOC_UPLOADER_ROLES)[number];

/**
 * Per-document pre-booking clearance review on a contract (Path B). Mirrors
 * BookingDocumentReview but keyed on contract_id (+ optional clearance cycle).
 * See §5.16.
 */
@Entity({ schema: 'freight', name: 'contract_document_review' })
@Index(['contractId'])
@Index(['status'])
export class ContractDocumentReview extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

  @ManyToOne(() => Contract, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'contract_id' })
  contract?: Contract;

  @Column({ name: 'clearance_cycle_id', type: 'uuid', nullable: true })
  clearanceCycleId?: string | null;

  @Column({ name: 'setting_code', type: 'varchar', length: 128 })
  settingCode!: string;

  @Column({ name: 'file_key', type: 'varchar', length: 128 })
  fileKey!: string;

  @Column({ name: 'file_record_id', type: 'uuid', nullable: true })
  fileRecordId?: string | null;

  @Column({ name: 'status', type: 'varchar', length: 20, default: 'PENDING' })
  status!: ContractDocReviewStatus;

  @Column({ name: 'note', type: 'text', nullable: true })
  note?: string | null;

  @Column({ name: 'uploaded_by_role', type: 'varchar', length: 20, default: 'CUSTOMER' })
  uploadedByRole!: ContractDocUploaderRole;

  @Column({ name: 'reviewed_by_staff_id', type: 'uuid', nullable: true })
  reviewedByStaffId?: string | null;

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