import {
  IsEmail,
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsPhoneNumber,

  IsString
} from "class-validator";
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { config } from '../../config';
import { Address } from "./Address";
import { IAddressUser } from './IAddressUser';
import { RunnerGroup } from "./RunnerGroup";

/**
 * Defines the GroupContact entity.
 * Mainly it's own class to reduce duplicate code and enable contact's to be associated with multiple groups.
*/
@Entity()
export class GroupContact implements IAddressUser {
  /**
   * Autogenerated unique id (primary key).
   */
  @PrimaryGeneratedColumn()
  @IsInt()
  id: number;

  /**
   * The contact's first name.
   */
  @Column()
  @IsNotEmpty()
  @IsString()
  firstname: string;

  /**
   * The contact's middle name.
   */
  @Column({ nullable: true })
  @IsOptional()
  @IsString()
  middlename?: string;

  /**
   * The contact's last name.
   */
  @Column()
  @IsNotEmpty()
  @IsString()
  lastname: string;

  /**
   * The contact's address.
   * This is a address object to prevent any formatting differences.
   */
  @IsOptional()
  @ManyToOne(() => Address, address => address.addressUsers, { nullable: true })
  address?: Address;

  /**
   * The contact's phone number.
   * This will be validated against the configured country phone numer syntax (default: international).
   */
  @Column({ nullable: true })
  @IsOptional()
  @IsPhoneNumber(config.phone_validation_countrycode)
  phone?: string;

  /**
   * The contact's email address.
   * Could later be used to automaticly send mails concerning the contact's associated groups.
   */
  @Column({ nullable: true })
  @IsOptional()
  @IsEmail()
  email?: string;

  /**
    * Used to link contacts to groups.
    */
  @OneToMany(() => RunnerGroup, group => group.contact, { nullable: true })
  groups: RunnerGroup[];

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