import { IsNotEmpty, IsOptional, IsPostalCode, IsString } from 'class-validator';
import { config } from '../../config';
import { Address } from '../entities/Address';

/**
 * This classed is used to create a new Address entity from a json body (post request).
 */
export class CreateAddress {
    /**
     * The newaddress's description.
     */
    @IsString()
    @IsOptional()
    description?: string;

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

    /**
     * The new address's second line.
     * Containing optional information.
     */
    @IsString()
    @IsOptional()
    address2?: string;

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

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

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

    /**
     * Creates a new Address entity from this.
     */
    public toAddress(): Address {
        let newAddress: Address = new Address();

        newAddress.address1 = this.address1;
        newAddress.address2 = this.address2;
        newAddress.postalcode = this.postalcode;
        newAddress.city = this.city;
        newAddress.country = this.country;

        return newAddress;
    }
}