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

export const CONTRACT_APPROVAL_STEP_STATUSES = [
  'PENDING',
  'APPROVED',
  'REJECTED',
  'SKIPPED',
] as const;
export type ContractApprovalStepStatus =
  (typeof CONTRACT_APPROVAL_STEP_STATUSES)[number];

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

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

  @Column({ name: 'step_order', type: 'smallint', default: 0 })
  stepOrder!: number;

  @Column({ name: 'required_role', type: 'varchar', length: 64 })
  requiredRole!: string;

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

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

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

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

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