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

/**
 * One row per started Fayda verification. Mirrors the passenger-api Prisma
 * model `FaydaVerificationSession`, but stored in the freight schema via
 * TypeORM. `state` is the single-use CSRF token that links the eSignet
 * redirect back to this session.
 */
@Entity({ name: 'fayda_verification_sessions', schema: 'freight' })
@Index(['expiresAt'])
@Index(['iamUserId'])
export class FaydaVerificationSession extends BaseEntity {
  @Column({ name: 'state', unique: true })
  state!: string;

  @Column({ name: 'code_verifier' })
  codeVerifier!: string;

  /** VERIFY | LOGIN */
  @Column({ name: 'purpose', default: 'VERIFY' })
  purpose!: string;

  /** WEB | MOBILE — recorded for audit */
  @Column({ name: 'platform', default: 'WEB' })
  platform!: string;

  @Column({ name: 'save_to_account', type: 'boolean', default: false })
  saveToAccount!: boolean;

  /** PENDING | COMPLETED | FAILED */
  @Column({ name: 'status', default: 'PENDING' })
  status!: string;

  @Column({ name: 'error_code', type: 'varchar', nullable: true })
  errorCode?: string | null;

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

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

  @Column({ name: 'expires_at', type: 'timestamptz' })
  expiresAt!: Date;

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