import { Column, Entity, JoinTable, ManyToMany, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { CharacterTag } from './character-tag';
import { InteractionTag } from './interaction-tag';
import { Work } from './work';
import { WorkCharacterName } from './work-character-name';
import { WorldCharacter } from './world-character';

/**
 * This entity describes a character in a work.
 * The character can be original or based on one or more existing characters.
 */
@Entity()
export class WorkCharacter implements IIdentifiableEntity, IMultiNamedEntity {
  @PrimaryGeneratedColumn()
  public id!: number;

  @Column({
    nullable: false,
    default: '',
  })
  public nameCanonical!: string;

  @OneToMany(() => WorkCharacterName, (workCharacterName: WorkCharacterName) => workCharacterName.entity)
  public names!: Promise<WorkCharacterName[]>;

  /**
   * the works the character is a part of
   * one work character can be part of multiple works because of series
   */
  @ManyToMany(() => Work, (work: Work) => work.workCharacters)
  public works!: Promise<Work[]>;

  /**
   * interaction with other characters as actor
   */
  @ManyToMany(() => InteractionTag, (interactionTag: InteractionTag) => interactionTag.subjectCharacters)
  @JoinTable()
  public interactWith!: Promise<InteractionTag[]>;

  /**
   * interaction with other characters as receiver
   */
  @ManyToMany(() => InteractionTag, (interactionTag: InteractionTag) => interactionTag.objectCharacters)
  @JoinTable()
  public interactedBy!: Promise<InteractionTag[]>;

  /**
   * tags connected to the character
   */
  @OneToMany(() => CharacterTag, (characterTag: CharacterTag) => characterTag.workCharacter)
  public characterTags!: Promise<CharacterTag[]>;

  /**
   * existing characters character is based on
   */
  @ManyToMany(() => WorldCharacter, (worldCharacter: WorldCharacter) => worldCharacter.workCharacters)
  @JoinTable()
  public worldCharacters!: Promise<WorldCharacter[]>;
}
