import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Unique,
  Index,
} from 'typeorm';
import { CommunicationStepEnum } from '../enum/communication-step.enum';

/**
 * Per-(program, step) template attachment (rev 10).
 * is_enabled = true means the master template fires when `step` occurs for the program.
 * A master may be attached to multiple steps (cross-step firing); the natural default
 * is the master attached to its own native step.
 */
@Entity('program_step_template_map')
@Unique('uq_pstm_program_step_master', ['programId', 'step', 'masterTemplateId'])
@Index('idx_pstm_program_step_enabled', ['programId', 'step', 'isEnabled'])
export class ProgramStepTemplateMap {
  @PrimaryGeneratedColumn('increment', { type: 'bigint' })
  id: number;

  @Column({ name: 'program_id', type: 'int', nullable: false })
  programId: number;

  @Column({ name: 'step', type: 'varchar', length: 64, nullable: false })
  step: CommunicationStepEnum;

  @Column({ name: 'master_template_id', type: 'bigint', nullable: false })
  masterTemplateId: number;

  @Column({ name: 'is_enabled', type: 'boolean', default: true })
  isEnabled: boolean;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
  createdAt: Date;

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
    onUpdate: 'CURRENT_TIMESTAMP',
  })
  updatedAt: Date;

  @Column({ name: 'created_by', type: 'bigint', nullable: true })
  createdBy: number | null;

  @Column({ name: 'updated_by', type: 'bigint', nullable: true })
  updatedBy: number | null;

  constructor(partial: Partial<ProgramStepTemplateMap>) {
    Object.assign(this, partial);
  }
}
