import { Column, Entity, JoinTable, ManyToMany, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { Source } from './source';
import { Work } from './work';

/**
 * A copy is the digital counterpart of a work.
 * It corresponds to a unique file or set of files which represent the work on the users device.
 *
 * Multiple works can have multiple copies (think of different scans of a physical work, or lossy compression).
 */
@Entity()
export class Copy implements IIdentifiableEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  /**
   * the work this entity is a copy of
   */
  @ManyToOne(() => Work, (work: Work) => work.copies, {
    nullable: false,
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  public original!: Promise<Work>;

  /**
   * where to find this specific copy
   */
  @ManyToMany(() => Source, (source: Source) => source.copies)
  @JoinTable()
  public sources!: Promise<Source[]>;

  /**
   * identifying hash of the file contents
   */
  @Column({
    nullable: false,
    default: '',
  })
  public hash!: string;

  /**
   * device location of the copy
   */
  @Column('text', {
    nullable: true,
  })
  public location!: string | null;

  /**
   * the ordering of the copies belonging to the same work,
   * lower number is higher ranked
   */
  @Column({
    nullable: false,
    default: 0,
  })
  public ranking!: number;
}
