import {
  IsInt,
  IsNotEmpty,
  IsOptional,
  IsPostalCode,
  IsString
} from "class-validator";
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { config } from '../../config';
import { IAddressUser } from './IAddressUser';

/**
 * Defines the Address entity.
 * Implemented this way to prevent any formatting differences.
*/
@Entity()
export class Address {
  /**
   * Autogenerated unique id (primary key).
   */
  @PrimaryGeneratedColumn()
  @IsInt()
  id: number;

  /**
   * The address's description.
   * Optional and mostly for UX.
   */
  @Column({ nullable: true })
  @IsString()
  @IsOptional()
  description?: string;

  /**
   * The address's first line.
   * Containing the street and house number.
   */
  @Column()
  @IsString()
  @IsNotEmpty()
  address1: string;

  /**
   * The address's second line.
   * Containing optional information.
   */
  @Column({ nullable: true })
  @IsString()
  @IsOptional()
  address2?: string;

  /**
   * The address's postal code.
   * This will get checked against the postal code syntax for the configured country.
   */
  @Column()
  @IsString()
  @IsNotEmpty()
  @IsPostalCode(config.postalcode_validation_countrycode)
  postalcode: string;

  /**
   * The address's city.
   */
  @Column()
  @IsString()
  @IsNotEmpty()
  city: string;

  /**
   * The address's country.
   */
  @Column()
  @IsString()
  @IsNotEmpty()
  country: string;

  /**
   * Used to link the address to participants.
   */
  @OneToMany(() => IAddressUser, addressUser => addressUser.address, { nullable: true })
  addressUsers: IAddressUser[];
}
