<?php
require_once APPPATH . 'models/Entities/CF_ContainerListArguments.php';
require_once APPPATH . 'models/Entities/CF_ContainerImageListArguments.php';
require_once APPPATH . 'models/Entities/CF_ContainerVolumeListArguments.php';

class Containers_model extends Cf_Model
{
    public const CONTAINERS_API_ENDPOINT = '/inventory/containers';
    public const CONTAINER_IMAGES_API_ENDPOINT = '/inventory/container-images';
    public const CONTAINER_VOLUMES_API_ENDPOINT = '/inventory/container-volumes';
    public const CONTAINERS_SUMMARY_API_ENDPOINT = '/inventory/containers/summary';

    public function listContainers(CF_ContainerListArguments $arguments)
    {
        try {
            return json_decode(
                $this->getRestClient()->get(
                    self::CONTAINERS_API_ENDPOINT . '?' . $arguments->toGetParameters(),
                    htmlSpecialCharsEncode: false,
                ),
                associative: true,
            );
        } catch (Exception $e) {
            log_message(log_level_for_exception($e), $e->getMessage() . ' ' . $e->getFile() . ' line:' . $e->getLine());
            throw $e;
        }
    }

    public function listImages(CF_ContainerImageListArguments $arguments)
    {
        try {
            return json_decode(
                $this->getRestClient()->get(
                    self::CONTAINER_IMAGES_API_ENDPOINT . '?' . $arguments->toGetParameters(),
                    htmlSpecialCharsEncode: false,
                ),
                associative: true,
            );
        } catch (Exception $e) {
            log_message(log_level_for_exception($e), $e->getMessage() . ' ' . $e->getFile() . ' line:' . $e->getLine());
            throw $e;
        }
    }

    public function listVolumes(CF_ContainerVolumeListArguments $arguments)
    {
        try {
            return json_decode(
                $this->getRestClient()->get(self::CONTAINER_VOLUMES_API_ENDPOINT . '?' . $arguments->toGetParameters()),
                associative: true,
            );
        } catch (Exception $e) {
            log_message(log_level_for_exception($e), $e->getMessage() . ' ' . $e->getFile() . ' line:' . $e->getLine());
            throw $e;
        }
    }

    public function summary(string $hostkey)
    {
        try {
            return json_decode(
                $this->getRestClient()->get(
                    self::CONTAINERS_SUMMARY_API_ENDPOINT . '/' . $hostkey,
                    htmlSpecialCharsEncode: false,
                ),
                associative: true,
            );
        } catch (Exception $e) {
            log_message(log_level_for_exception($e), $e->getMessage() . ' ' . $e->getFile() . ' line:' . $e->getLine());
            throw $e;
        }
    }
}
