import { Column, CreateDateColumn, Entity, Index, PrimaryColumn, UpdateDateColumn } from 'typeorm';
import type { ImportRunSource, ImportRunStatus, ImportStepKey } from '../imports.types';

@Entity('import_runs')
@Index('idx_import_runs_user_created', ['userId', 'createdAt'])
export class ImportRun {
  @PrimaryColumn({ type: 'varchar', length: 36 })
  id: string;

  @Column({ name: 'user_id', type: 'integer' })
  userId: number;

  @Column({ name: 'conversation_id', type: 'integer', nullable: true })
  conversationId: number | null;

  @Column({ type: 'varchar', length: 10, default: 'manual' })
  source: ImportRunSource;

  @Column({ name: 'file_name', type: 'varchar', length: 255 })
  fileName: string;

  /** Serialized sheet data — parsed rows are kept here for v1. */
  @Column({ name: 'sheets_json', type: 'mediumtext' })
  sheetsJson: string;

  /** Serialized ImportRunSettings — confirmed mapping/policies from AI preflight. */
  @Column({ name: 'settings_json', type: 'text', nullable: true })
  settingsJson: string | null;

  @Column({ type: 'varchar', length: 20, default: 'preparing' })
  status: ImportRunStatus;

  @Column({ name: 'current_step_key', type: 'varchar', length: 20, nullable: true })
  currentStepKey: ImportStepKey | null;

  @Column({ type: 'varchar', length: 500, nullable: true })
  error: string | null;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
