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

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

@Entity({ schema: 'freight', name: 'booking_contract_signatures' })
@Unique(['bookingId', 'signerRole'])
@Index(['bookingId'])
export class BookingContractSignature extends BaseEntity {
  @Column({ name: 'booking_id', type: 'uuid' })
  bookingId!: string;

  @ManyToOne(() => Booking, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'booking_id' })
  booking?: Booking;

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

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

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

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

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

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

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

  @Column({ name: 'ip_address', type: 'varchar', length: 64, nullable: true })
  ipAddress?: string | null;
}
