import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { PercentCheck } from '../decorators/percent-check';
import { Tag } from './tag';
import { Work } from './work';

/**
 * This tag entity tags a work.
 */
@Entity()
@PercentCheck('weight')
export class WorkTag implements IIdentifiableEntity, IWeightedEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  /**
   * the describing tag
   */
  @ManyToOne(() => Tag, (tag: Tag) => tag.workTags, {
    nullable: false,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  public tag!: Promise<Tag>;

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

  @Column('int', {
    nullable: true,
  })
  public weight!: number | null;
}
