<?php
namespace Incoviba\Controller\API;

use Incoviba\Common\Ideal\Controller;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Incoviba\Service;

class Sociedades extends Controller
{
    use withJson;

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $output = [
            'sociedades' => $sociedadService->getAll('nombre'),
        ];
        return $this->withJson($response, $output);
    }
    public function getMany(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'sociedades' => [],
        ];
        foreach ($input['sociedades_ruts'] as $rut) {
            $sociedad = $sociedadService->getByRut($rut);
            if ($sociedad === null) {
                continue;
            }
            $output['sociedades'][] = $sociedad;
        }
        return $this->withJson($response, $output);
    }
    public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService, int $sociedad_rut): ResponseInterface
    {
        $output = [
            'sociedad_rut' => $sociedad_rut,
            'sociedad' => $sociedadService->getByRut($sociedad_rut),
        ];
        return $this->withJson($response, $output);
    }
    public function add(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'sociedad' => $sociedadService->add($input),
        ];
        return $this->withJson($response, $output);
    }
    public function addMany(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService, Service\Persona $personaService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'sociedades' => [],
        ];
        try {
            if (json_decode($input) !== null) {
                $input = json_decode($input, true);
            }
        } catch (\TypeError) {}
        foreach ($input['sociedades'] as $sociedadData) {
            try {
                if (json_decode($sociedadData) !== null) {
                    $sociedadData = json_decode($sociedadData, true);
                }
            } catch (\TypeError) {}
            $contacto = $personaService->add($sociedadData['contacto']);
            $sociedadData['contacto_rut'] = $contacto->rut;
            unset($sociedadData['contacto']);
            $sociedad = $sociedadService->add($sociedadData);
            if ($sociedad === null) {
                continue;
            }
            $output['sociedades'][] = $sociedad;
        }
        return $this->withJson($response, $output);
    }
    public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'sociedades' => [],
        ];
        foreach ($input['sociedades'] as $sociedadData) {
            $sociedad = $sociedadService->edit($sociedadData['rut'], $sociedadData);
            if ($sociedad === null) {
                continue;
            }
            $output['sociedades'][] = $sociedad;
        }
        return $this->withJson($response, $output);
    }
    public function delete(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'sociedades' => [],
        ];
        foreach ($input['sociedades_ruts'] as $rut) {
            $sociedad = $sociedadService->getByRut($rut);
            $deleted = $sociedadService->delete($rut);
            $output['sociedades'][] = [
                'sociedad' => $sociedad,
                'deleted' => $deleted,
            ];
        }
        return $this->withJson($response, $output);
    }

    public function asignar(ServerRequestInterface $request, ResponseInterface $response, Service\Sociedad $sociedadService): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'proveedores' => [],
        ];
        foreach ($input['proveedores'] as $proveedorData) {
            $proveedor = $sociedadService->asignar($proveedorData['inmobiliaria_rut'], $proveedorData['sociedad_rut']);
            if ($proveedor === null) {
                continue;
            }
            $output['proveedores'][] = $proveedor;
        }
        return $this->withJson($response, $output);
    }
}
