import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { WorkflowConfiguration } from './workflow-configuration.entity';
import { User } from './user.entity';
import { RegistrationWorkflowStage } from './registration-workflow-stage.entity';

@Entity('workflow_stage')
@Index('idx_workflow_stage_workflow_order', ['workflowConfiguration', 'stageOrder'])
@Index('idx_workflow_stage_workflow_stage_key', ['workflowConfiguration', 'stageKey'], { unique: true })
export class WorkflowStage {

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'workflow_config_id', type: 'int' })
  workflowConfigId: number;

  @ManyToOne(() => WorkflowConfiguration, workflowConfiguration => workflowConfiguration.stages, {
    nullable: false
  })
  @JoinColumn({ name: 'workflow_config_id' })
  workflowConfiguration: WorkflowConfiguration;

  @Column({ name: 'stage_key', type: 'varchar', length: 100 })
  stageKey: string;

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

  @Column({ name: 'stage_order', type: 'int' })
  stageOrder: number;

  @Column({ name: 'stage_category', type: 'varchar', length: 100 })
  stageCategory: string;

  @Column({ name: 'is_optional', type: 'boolean', default: false })
  isOptional: boolean;

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

  @Column({ type: 'jsonb', nullable: true })
  permissions: Record<string, unknown> | null;

  @Column({ name: 'prerequisite_rules_config', type: 'jsonb', nullable: true })
  prerequisiteRulesConfig: Record<string, unknown> | null;

  @Column({ name: 'execution_rules_config', type: 'jsonb', nullable: true })
  executionRulesConfig: Record<string, unknown> | null;

  @Column({ name: 'action_buttons', type: 'jsonb', nullable: true })
  actionButtons: Record<string, unknown>[] | null;

  @Column({ name: 'next_stages', type: 'jsonb', nullable: true })
  nextStages: Record<string, unknown>[] | null;

  @Column({ name: 'completion_actions_config', type: 'jsonb', nullable: true })
  completionActionsConfig: Record<string, unknown> | null;

  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, unknown> | null;

  @OneToMany(() => RegistrationWorkflowStage, registrationStage => registrationStage.workflowStage)
  registrationWorkflowStages: RegistrationWorkflowStage[];

  @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;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date | null;

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

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

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'created_by' })
  createdBy: User | null;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'updated_by' })
  updatedBy: User | null;

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