import {
    IsEnum,
    IsInt,
    IsNotEmpty,
    IsObject,
    IsPositive
} from "class-validator";
import { Permission } from '../entities/Permission';
import { PermissionAction } from '../enums/PermissionAction';
import { PermissionTarget } from '../enums/PermissionTargets';
import { ResponseObjectType } from '../enums/ResponseObjectType';
import { IResponse } from './IResponse';
import { ResponsePrincipal } from './ResponsePrincipal';

/**
 * Defines the permission response.
*/
export class ResponsePermission implements IResponse {
    /**
    * The responseType.
    * This contains the type of class/entity this response contains.
    */
    responseType: ResponseObjectType = ResponseObjectType.PERMISSION;

    /**
     * The permission's id.
     */
    @IsInt()
    id: number;;

    /**
     * The permissions's principal.
     */
    @IsObject()
    @IsNotEmpty()
    principal: ResponsePrincipal;

    /**
     * The permissions's target.
     */
    @IsNotEmpty()
    @IsEnum(PermissionTarget)
    target: PermissionTarget;

    /**
     * The permissions's action.
     */
    @IsNotEmpty()
    @IsEnum(PermissionAction)
    action: PermissionAction;

    @IsInt()
    @IsPositive()
    created_at: number;

    @IsInt()
    @IsPositive()
    updated_at: number;

    /**
     * Creates a ResponsePermission object from a permission.
     * @param permission The permission the response shall be build for.
     */
    public constructor(permission: Permission) {
        this.id = permission.id;
        this.principal = permission.principal.toResponse();
        this.target = permission.target;
        this.action = permission.action;
        this.created_at = permission.created_at;
        this.updated_at = permission.updated_at;
    }
}
