<?php
require_once APPPATH . 'modules/groups_app/models/entities/CF_SharedGroup.php';

class Shared_groups_model extends Cf_Model
{
    private $endpoint = '/host-groups/shared';
    private $myGroupsEndpoint = '/host-groups/my';

    public function create($data)
    {
        return $this->getRestClient()->post($this->endpoint, $data);
    }

    public function update($id, $data)
    {
        return $this->getRestClient()->patch("{$this->endpoint}/$id", $data);
    }

    public function makePersonal($id)
    {
        return $this->getRestClient()->post("{$this->endpoint}/$id/makePersonal");
    }

    public function remove($id)
    {
        return $this->getRestClient()->delete("{$this->endpoint}/$id");
    }

    public function get($id)
    {
        try {
            $response = $this->getRestClient()->get("{$this->endpoint}/$id");
            $data = is_string($response) ? json_decode($response, true) : (array) $response;
            return new CF_SharedGroup($data);
        } catch (HttpClient_NotFound $e) {
            return null;
        }
    }

    public function list($params)
    {
        $query = http_build_query($params);
        return $this->getRestClient()->get("{$this->endpoint}?$query");
    }

    public function addToFavorites($id)
    {
        return $this->getRestClient()->post("{$this->endpoint}/$id/favorite");
    }

    public function removeFromFavorites($id)
    {
        return $this->getRestClient()->delete("{$this->endpoint}/$id/favorite");
    }
}
