<?php

namespace Containers;

use Containers\Dto\ContainerListArgumentsDto;
use Containers\Dto\ContainerImageListArgumentsDto;
use Containers\Dto\ContainerVolumeListArgumentsDto;
use Containers\Models\ContainersModel;
use Containers\Validators\ContainerRequestValidator;

/**
 * @uri /inventory/containers
 */
class ContainersList extends \CfProtectedResource
{
    /**
     * @rbacAlias query.post
     */
    public function get($request): \Response
    {
        $response = new \Response($request);
        try {
            $args = ContainerRequestValidator::validateContainerList($_GET);
            $dto = ContainerListArgumentsDto::fromArray($args);
            $model = new ContainersModel($this->username);
            $response->code = \Response::OK;
            $response->body = json_encode($model->listContainers($dto));
        } catch (\InvalidArgumentException $e) {
            $response->code = \Response::BADREQUEST;
            $response->body = $e->getMessage();
        }
        return $response;
    }
}

/**
 * @uri /inventory/container-images
 */
class ContainerImagesList extends \CfProtectedResource
{
    /**
     * @rbacAlias query.post
     */
    public function get($request): \Response
    {
        $response = new \Response($request);
        try {
            $args = ContainerRequestValidator::validateImageList($_GET);
            $dto = ContainerImageListArgumentsDto::fromArray($args);
            $model = new ContainersModel($this->username);
            $response->code = \Response::OK;
            $response->body = json_encode($model->listImages($dto));
        } catch (\InvalidArgumentException $e) {
            $response->code = \Response::BADREQUEST;
            $response->body = $e->getMessage();
        }
        return $response;
    }
}

/**
 * @uri /inventory/container-volumes
 */
class ContainerVolumesList extends \CfProtectedResource
{
    /**
     * @rbacAlias query.post
     */
    public function get($request): \Response
    {
        $response = new \Response($request);
        try {
            $args = ContainerRequestValidator::validateVolumeList($_GET);
            $dto = ContainerVolumeListArgumentsDto::fromArray($args);
            $model = new ContainersModel($this->username);
            $response->code = \Response::OK;
            $response->body = json_encode($model->listVolumes($dto));
        } catch (\InvalidArgumentException $e) {
            $response->code = \Response::BADREQUEST;
            $response->body = $e->getMessage();
        }
        return $response;
    }
}

/**
 * @uri /inventory/containers/summary/:hostkey
 */
class ContainersSummary extends \CfProtectedResource
{
    /**
     * @rbacAlias query.post
     */
    public function get($request, string $hostkey): \Response
    {
        $response = new \Response($request);
        try {
            $this->checkHostAccessAndReturnName($this->username, $hostkey);
            $model = new ContainersModel($this->username);
            $response->code = \Response::OK;
            $response->body = json_encode($model->summary($hostkey)->toArray());
        } catch (\InvalidArgumentException $e) {
            $response->code = \Response::BADREQUEST;
            $response->body = $e->getMessage();
        }
        return $response;
    }
}
