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

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement;
use Incoviba\Controller\API\withJson;
use Incoviba\Common\Ideal;
use Incoviba\Repository;
use Incoviba\Service;

class Proveedores extends Ideal\Controller
{
    use withJson;

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response,
                             Service\Inmobiliaria\Proveedor $proveedorService): ResponseInterface
    {
        $output = ['proveedores' => []];
        try {
            $output['proveedores'] = $proveedorService->getAll('nombre');
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function get(ServerRequestInterface $request, ResponseInterface $response,
                        Service\Inmobiliaria\Proveedor $proveedorService, int $proveedor_rut): ResponseInterface
    {
        $output = [
            'proveedor_rut' => $proveedor_rut,
            'proveedor' => null,
        ];
        try {
            $output['proveedor'] = $proveedorService->getById($proveedor_rut);
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function add(ServerRequestInterface $request, ResponseInterface $response,
                        Service\Inmobiliaria\Proveedor $proveedorService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'proveedores' => [],
            'success' => [],
        ];
        foreach ($input['proveedores'] as $json) {
            $data = json_decode($json, true);
            try {
                $output['proveedores'] []= $proveedorService->add($data);
                $output['success'] []= true;
            } catch (Implement\Exception\EmptyResult) {
                $output['success'] []= false;
            }
        }
        return $this->withJson($response, $output);
    }
    public function edit(ServerRequestInterface $request, ResponseInterface $response,
                         Service\Inmobiliaria\Proveedor $proveedorService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'proveedores' => [],
            'success' => [],
        ];
        foreach ($input['proveedores'] as $json) {
            $data = json_decode($json, true);
            try {
                $proveedor = $proveedorService->getById($data['rut']);
                $output['proveedores'] []= $proveedorService->edit($proveedor, $data);
                $output['success'] []= true;
            } catch (Implement\Exception\EmptyResult) {
                $output['success'] []= false;
            }
        }
        return $this->withJson($response, $output);
    }
    public function delete(ServerRequestInterface $request, ResponseInterface $response,
                           Service\Inmobiliaria\Proveedor $proveedorService, int $proveedor_rut): ResponseInterface
    {
        $output = [
            'proveedor_rut' => $proveedor_rut,
            'proveedor' => null,
            'success' => false
        ];
        try {
            $output['proveedor'] = $proveedorService->getById($proveedor_rut);
            $output['success'] = $proveedorService->delete($output['proveedor']);
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
