import { Entity, JoinTable, ManyToMany, OneToMany } from 'typeorm';
import { BaseEntity } from '../base-entity';
import { Author } from './author';
import { MultiName, MultiNamed } from './base/multi-named';
import { Character } from './character';
import { Copy } from './copy';
import { Fiction } from './fiction';
import { Tag } from './tag';

@Entity()
export class Book extends MultiNamed(BaseEntity, 'BookMultiName') {
  @OneToMany(
    () => Copy,
    (copy: Copy) => copy.original
  )
  public copies: Promise<Copy[]>;

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

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

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

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

@Entity()
export class BookMultiName extends MultiName('Book') {}
