import { AudioOptionItem } from '../../../repositories/audio_option_items/entity/audio_option_item.entity';
import { AudioFile } from '../../../repositories/audio_files/entity/audio_file.entity';
import { User } from '../../../repositories/users/entity/user.entity';
import { TemplateFile } from '../../template_files/entity/template_file.entity';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  OneToOne,
  JoinColumn,
  OneToMany,
  ManyToOne,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { bigintTransformer } from '../../../common/entity';

@Entity({ name: 'tasks' })
export class Task {
  @PrimaryGeneratedColumn()
  id: number;
  @Column()
  job_number: string;
  @Column()
  account_id: number;
  @Column({ nullable: true, type: 'boolean' })
  is_job_number_enabled: boolean | null;
  @Column()
  audio_file_id: number;
  @Column()
  status: string;
  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  typist_user_id: number | null;
  @Column()
  priority: string;
  @Column({ nullable: true, type: 'bigint', transformer: bigintTransformer })
  template_file_id: number | null;
  @Column({ nullable: true, type: 'datetime' })
  started_at: Date | null;
  @Column({ nullable: true, type: 'datetime' })
  finished_at: Date | 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;
  @OneToOne(() => AudioFile, (audiofile) => audiofile.task)
  @JoinColumn({ name: 'audio_file_id' })
  file: AudioFile | null;
  @OneToMany(() => AudioOptionItem, (option) => option.task)
  option_items: AudioOptionItem[] | null;
  @OneToOne(() => User, (user) => user.id)
  @JoinColumn({ name: 'typist_user_id' })
  typist_user: User | null;
  @ManyToOne(() => TemplateFile, (templateFile) => templateFile.id)
  @JoinColumn({ name: 'template_file_id' })
  template_file: TemplateFile | null;
}
