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

export const CONTRACT_SIGNER_ROLES = ['CUSTOMER', 'STAFF', 'DIRECTOR', 'CEO'] as const;
export type ContractSignerRole = (typeof CONTRACT_SIGNER_ROLES)[number];

@Entity({ schema: 'freight', name: 'contract_signatures' })
@Index(['contractId'])
export class ContractSignature extends BaseEntity {
  @Column({ name: 'contract_id', type: 'uuid' })
  contractId!: string;

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

  @Column({ name: 'role', type: 'varchar', length: 20 })
  role!: ContractSignerRole;

  @Column({ name: 'signer_display_name', type: 'varchar', length: 255 })
  signerDisplayName!: string;

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

  @ManyToOne(() => FileRecord, { nullable: true })
  @JoinColumn({ name: 'signature_file_id' })
  signatureFile?: FileRecord | null;

  /** Company stamp/seal image, uploaded alongside the drawn signature. */
  @Column({ name: 'stamp_file_id', type: 'uuid', nullable: true })
  stampFileId?: string | null;

  @ManyToOne(() => FileRecord, { nullable: true })
  @JoinColumn({ name: 'stamp_file_id' })
  stampFile?: FileRecord | null;

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

  @Column({ name: 'signed_at', type: 'timestamptz', default: () => 'now()' })
  signedAt!: Date;
}
