import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
import { WorkflowTypist } from './workflow_typists.entity';
import { Worktype } from '../../worktypes/entity/worktype.entity';
import { TemplateFile } from '../../template_files/entity/template_file.entity';
import { User } from '../../users/entity/user.entity';
import { bigintTransformer } from '../../../common/entity';

@Entity({ name: 'workflows' })
export class Workflow {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  account_id: number;

  @Column()
  author_id: number;

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

  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  template_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(() => User, (user) => user.id)
  @JoinColumn({ name: 'author_id' })
  author: User | null;

  @ManyToOne(() => Worktype, (worktype) => worktype.id)
  @JoinColumn({ name: 'worktype_id' })
  worktype: Worktype | null;

  @ManyToOne(() => TemplateFile, (templateFile) => templateFile.id)
  @JoinColumn({ name: 'template_id' })
  template: TemplateFile | null;

  @OneToMany(() => WorkflowTypist, (workflowTypist) => workflowTypist.workflow)
  workflowTypists: WorkflowTypist[] | null;
}
