import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Workflow } from './workflow.entity';
import { User } from '../../users/entity/user.entity';
import { UserGroup } from '../../user_groups/entity/user_group.entity';
import { bigintTransformer } from '../../../common/entity';

@Entity({ name: 'workflow_typists' })
export class WorkflowTypist {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  workflow_id: number;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  typist_id: number | null;

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  typist_group_id: number | null;

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

  @CreateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  }) // defaultはSQLite用設定値.本番用は別途migrationで設定
  created_at: Date;

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

  @UpdateDateColumn({
    default: () => "datetime('now', 'localtime')",
    type: 'datetime',
  }) // defaultはSQLite用設定値.本番用は別途migrationで設定
  updated_at: Date;

  @ManyToOne(() => Workflow, (workflow) => workflow.id)
  @JoinColumn({ name: 'workflow_id' })
  workflow: Workflow | null;

  @ManyToOne(() => User, (user) => user.id)
  @JoinColumn({ name: 'typist_id' })
  typist: User | null;

  @ManyToOne(() => UserGroup, (userGroup) => userGroup.id)
  @JoinColumn({ name: 'typist_group_id' })
  typistGroup: UserGroup | null;
}
