<?php
namespace Incoviba\Controller\API\Proyectos;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Controller\API\withJson;
use Incoviba\Exception\ServiceAction;
use Incoviba\Service;

class Brokers
{
    use withJson;

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
    {
        $brokers = $brokerService->getAll();

        return $this->withJson($response, compact('brokers'));
    }
    public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
    {
        try {
            $broker = $brokerService->get($broker_rut);
            return $this->withJson($response, compact('broker'));
        } catch (ServiceAction\Read $exception) {
            return $this->withError($response, $exception);
        }

    }
    public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'brokers' => [],
            'success' => false,
            'partial' => false,
            'errors' => []
        ];
        foreach ($body['brokers'] as $jsonData) {
            try {
                $data = json_decode($jsonData, true);
                if (is_array($jsonData)) {
                    $data = $jsonData;
                }
                $output['brokers'] []= [
                    'broker' => $brokerService->add($data),
                    'success' => true
                ];
                $output['partial'] = true;
            } catch (ServiceAction\Create $exception) {
                $output['errors'] []= $this->parseError($exception);
            }
        }
        if (count($output['brokers']) == count($body['brokers'])) {
            $output['success'] = true;
        }

        return $this->withJson($response, $output);
    }
    public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'brokers' => [],
            'success' => false,
            'partial' => false,
            'errors' => []
        ];
        foreach ($body['brokers'] as $data) {
            try {
                $data = json_decode($data, true);
                $output['brokers'] []= [
                    'rut' => $data['rut'],
                    'broker' => $brokerService->edit($data),
                    'success' => true
                ];
                $output['partial'] = true;
            } catch (ServiceAction\Update $exception) {
                $output['errors'] []= $this->parseError($exception);
            }
        }
        if (count($output['brokers']) == count($body['brokers'])) {
            $output['success'] = true;
        }

        return $this->withJson($response, $output);
    }
    public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Proyecto\Broker $brokerService, int $broker_rut): ResponseInterface
    {
        $output = [
            'broker_rut' => $broker_rut,
            'broker' => null,
            'success' => false
        ];
        try {
            $output['broker'] = $brokerService->delete($broker_rut);
            $output['success'] = true;
        } catch (ServiceAction\Delete $exception) {
            return $this->withError($response, $exception);
        }

        return $this->withJson($response, $output);
    }
}
