<?php

namespace AuthStack\UserDirectory\Service;

use AuthStack\UserDirectory\Exceptions\OrganizationException;
use AuthStack\UserDirectory\Model\Organization as OrganizationModel;

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

        }, OrganizationException::class);
    }

    /**
     * @param array $model
     * @return mixed
     */
    public function createForAccount(array $model, int $id)
    {
        return $this->remote(function() use (&$model, $id)
        {
            return new OrganizationModel($this->json('create_for_account', array_merge($model, ['account_id' => $id])));

        }, OrganizationException::class);
    }

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

        }, OrganizationException::class);
    }

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

            return $response['result'];

        }, OrganizationException::class);
    }

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

            return $response['result'];

        }, OrganizationException::class);
    }

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

            return $response;

        }, OrganizationException::class);
    }

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

            return $response['result'];

        }, OrganizationException::class);
    }

    /**
     * @param int $id
     * @param array $options
     * @return mixed
     */
    public function members(int $id, array $options = [])
    {
        return $this->remote(function() use ($id, &$options)
        {
            $response = $this->json('members', null, [$id], $options);

            return $response;

        }, OrganizationException::class);
    }

    /**
     * @param int $id
     * @return array
     */
    public function groups(int $id): array
    {
        return $this->remote(function() use ($id)
        {
            $response = $this->json('members', null, [$id]);

            return $response;

        }, OrganizationException::class);
    }
}