import { Column, Entity, JoinTable, ManyToMany, ManyToOne, OneToMany } from 'typeorm';
import { BaseEntity } from '../base-entity';
import { Rateable } from './base/rateable';
import { Book } from './book';
import { CopyType } from './copy-type';
import { Language } from './language';
import { Source } from './source';
import { Translator } from './translator';

@Entity()
export class Copy extends Rateable(BaseEntity) {
  @ManyToOne(
    () => Book,
    (book: Book) => book.copies,
    {
      nullable: false,
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    }
  )
  public original: Promise<Book>;

  @Column({ nullable: false, default: false })
  public favorited: boolean;

  @OneToMany(
    () => CopyType,
    (copyType: CopyType) => copyType.copy
  )
  public types: Promise<CopyType[]>;

  @Column({ nullable: false })
  public isDigital: boolean;

  @ManyToMany(() => Source, {
    nullable: false,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinTable()
  public sources: Promise<Source[]>;

  @ManyToMany(() => Language, {
    nullable: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinTable()
  public languages: Promise<Language[]>;

  @ManyToMany(() => Translator, {
    nullable: true,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinTable()
  public translators: Promise<Translator[]>;
}
