import { IsBoolean, IsObject, IsOptional } from 'class-validator';
import * as uuid from 'uuid';
import { Address } from '../../entities/Address';
import { RunnerOrganization } from '../../entities/RunnerOrganization';
import { CreateRunnerGroup } from './CreateRunnerGroup';


/**
 * This classed is used to create a new RunnerOrganization entity from a json body (post request).
 */
export class CreateRunnerOrganization extends CreateRunnerGroup {
    /**
     * The new organization's address.
     */
    @IsOptional()
    @IsObject()
    address?: Address;

    /**
     * Is registration enabled for the new organization?
     */
    @IsOptional()
    @IsBoolean()
    registrationEnabled?: boolean = false;

    /**
     * Creates a new RunnerOrganization entity from this.
     */
    public async toEntity(): Promise<RunnerOrganization> {
        let newRunnerOrganization: RunnerOrganization = new RunnerOrganization();

        newRunnerOrganization.name = this.name;
        newRunnerOrganization.contact = await this.getContact();
        newRunnerOrganization.address = this.address;
        Address.validate(newRunnerOrganization.address);

        if (this.registrationEnabled) {
            newRunnerOrganization.key = uuid.v4().toUpperCase();
        }

        return newRunnerOrganization;
    }
}