import {
  IsInt,
  IsNotEmpty
} from "class-validator";
import { Entity, ManyToOne, PrimaryGeneratedColumn, TableInheritance } from "typeorm";
import { ResponseDonation } from '../responses/ResponseDonation';
import { Donor } from './Donor';

/**
 * Defines the Donation entity.
 * A donation just associates a donor with a donation amount.
 * The specifics of the amoun's determination has to be implemented in child classes.
*/
@Entity()
@TableInheritance({ column: { name: "type", type: "varchar" } })
export abstract class Donation {
  /**
   * Autogenerated unique id (primary key).
   */
  @PrimaryGeneratedColumn()
  @IsInt()
  id: number;

  /**
   * The donations's donor.
   */
  @IsNotEmpty()
  @ManyToOne(() => Donor, donor => donor.donations)
  donor: Donor;

  /**
   * The donation's amount in cents (or whatever your currency's smallest unit is.).
   * The exact implementation may differ for each type of donation.
   */
  public abstract get amount(): number;


  /**
   * Turns this entity into it's response class.
   */
  public toResponse(): ResponseDonation {
    return new ResponseDonation(this);
  }
}