import { IsArray, IsNotEmpty, IsOptional, IsString } from "class-validator";
import { UserGroup } from '../entities/UserGroup';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponsePrincipal } from './ResponsePrincipal';

/**
 * Defines the userGroup response.
*/
export class ResponseUserGroup extends ResponsePrincipal implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.USERGROUP;

    /**
     * The userGroup's name.
     */
    @IsNotEmpty()
    @IsString()
    name: string;

    /**
     * The userGroup's description.
     */
    @IsOptional()
    @IsString()
    description?: string;

    /**
     * The userGroup's permissions.
     */
    @IsArray()
    @IsOptional()
    permissions: string[] = new Array<string>();

    /**
     * Creates a ResponseUserGroup object from a userGroup.
     * @param group The userGroup the response shall be build for.
     */
    public constructor(group: UserGroup) {
        super(group);
        this.name = group.name;
        this.description = group.description;
        if (group.permissions) {
            for (let permission of group.permissions) {
                this.permissions.push(permission.toString());
            }
        }
    }
}
