<?php

namespace AuthStack\UserDirectory\Service;

use AuthStack\UserDirectory\Exceptions\PolicyException;
use AuthStack\UserDirectory\Model\Policy as PolicyModel;

class Policy extends RESTService
{
    /**
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return $this->remote(function() use (&$data): PolicyModel
        {
            return new PolicyModel($this->json('create', $data));

        }, PolicyException::class);
    }

    /**
     * @param int $id
     * @return PolicyModel
     */
    public function read(int $id): PolicyModel
    {
        return $this->remote(function() use ($id)
        {
            return new PolicyModel($this->json('read', [], [$id]));

        }, PolicyException::class);
    }

    /**
     * @param int $id
     * @param array $data
     * @return bool
     */
    public function update(int $id, array $data): bool
    {
        return $this->remote(function() use ($id, &$data)
        {
            $result = $this->json('update', $data, [$id]);

            return $result['result'];

        }, PolicyException::class);
    }

    /**
     * @param int $id
     * @return bool
     */
    public function delete(int $id): bool
    {
        return $this->remote(function() use ($id)
        {
            $result = $this->json('delete', [], [$id]);

            return $result['result'];

        }, PolicyException::class);
    }

    /**
     * @param array $options
     * @return array
     */
    public function listing(array $options = []): array
    {
        return $this->remote(function() use (&$options)
        {
            $result = $this->json('listing', [], [], $options);

            return $result;

        }, PolicyException::class);
    }
}