import { Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Author } from './author';
import { AuthorRole } from './author-role';
import { Work } from './work';

/**
 * This entity connects authors with their work and their role therein.
 */
@Entity()
export class WorkAuthor implements IIdentifiableEntity, IOrderableEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  /**
   * the work
   */
  @ManyToOne(() => Work, (work: Work) => work.workAuthors, {
    nullable: false,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  public work!: Promise<Work>;

  /**
   * the roles of the author in the work
   */
  @ManyToMany(() => AuthorRole, (authorRole: AuthorRole) => authorRole.workAuthors)
  @JoinTable()
  public authorRoles!: Promise<AuthorRole[]>;

  /**
   * the author
   */
  @ManyToOne(() => Author, (author: Author) => author.workAuthors, {
    nullable: false,
    onDelete: 'RESTRICT',
    onUpdate: 'CASCADE',
  })
  public author!: Promise<Author>;

  @Column({
    nullable: false,
    default: 0,
  })
  public order!: number;
}
