import {
  IsBoolean,
  IsEAN,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsString
} from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Runner } from "./Runner";
import { TrackScan } from "./TrackScan";

/**
 * Defines the RunnerCard entity.
 * A runnerCard is a physical representation for a runner.
 * It can be associated with a runner to create scans via the scan station's.
*/
@Entity()
export class RunnerCard {
  /**
   * Autogenerated unique id (primary key).
   */
  @PrimaryGeneratedColumn()
  @IsInt()
  id: number;

  /**
   * The card's currently associated runner.
   * To increase reusability a card can be reassigned.
   */
  @IsOptional()
  @ManyToOne(() => Runner, runner => runner.cards, { nullable: true })
  runner: Runner;

  /**
   * The card's code.
   * This has to be able to being converted to something barcode compatible.
   * Will get automaticlly generated (not implemented yet).
   */
  @Column()
  @IsEAN()
  @IsString()
  @IsNotEmpty()
  code: string;

  /**
   * Is the card enabled (for fraud reasons)?
   * Default: true
   */
  @Column()
  @IsBoolean()
  enabled: boolean = true;

  /**
   * The card's associated scans.
   * Used to link cards to track scans.
   */
  @OneToMany(() => TrackScan, scan => scan.track, { nullable: true })
  scans: TrackScan[];
}
