<?php
namespace Incoviba\Controller\API;

use DateTimeImmutable;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\HttpResponse;
use Incoviba\Service;

class Nubox
{
    use withJson;

    public function token(ServerRequestInterface $request, ResponseInterface $response,
                          Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
    {
        $output = [
            'inmobiliaria_rut' => $inmobiliaria_rut,
            'token' => ''
        ];
        try {
            $output['token'] = $nuboxService->getToken($inmobiliaria_rut);
        } catch (HttpResponse $exception) {
            $output['error'] = [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage()
            ];
        }
        return $this->withJson($response, $output);
    }
    public function sistemas(ServerRequestInterface $request, ResponseInterface $response,
                             Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
    {
        $output = [
            'inmobiliaria_rut' => $inmobiliaria_rut,
            'sistemas' => []
        ];
        try {
            $output['sistemas'] = $nuboxService->getSistemas($inmobiliaria_rut);
        } catch (HttpResponse $exception) {
            $output['error'] = [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage()
            ];
        }
        return $this->withJson($response, $output);
    }
    public function libroMayor(ServerRequestInterface $request, ResponseInterface $response,
                               Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'inmobiliaria_rut' => $inmobiliaria_rut,
            'input' => $body,
            'libro_mayor' => []
        ];
        try {
            $from = new DateTimeImmutable($body['inicio']);
            $to = new DateTimeImmutable($body['termino']);
            $output['libro_mayor'] = $nuboxService->getLibroMayor($inmobiliaria_rut, $from, $to);
        } catch (HttpResponse $exception) {
            $output['error'] = [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage()
            ];
        }
        return $this->withJson($response, $output);
    }
    public function libroDiario(ServerRequestInterface $request, ResponseInterface $response,
                                Service\Contabilidad\Nubox $nuboxService, int $inmobiliaria_rut): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'inmobiliaria_rut' => $inmobiliaria_rut,
            'input' => $body,
            'libro_diario' => []
        ];
        try {
            $from = new DateTimeImmutable($body['inicio']);
            $to = new DateTimeImmutable($body['termino']);
            $output['libro_diario'] = $nuboxService->getLibroDiario($inmobiliaria_rut, $from, $to);
        } catch (HttpResponse $exception) {
            $output['error'] = [
                'code' => $exception->getCode(),
                'message' => $exception->getMessage()
            ];
        }
        return $this->withJson($response, $output);
    }
}
