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

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

class Cartolas extends Controller
{
    use withJson;

    public function procesar(ServerRequestInterface $request, ResponseInterface $response,
                                    Repository\Inmobiliaria $inmobiliariaRepository,
                                    Repository\Banco $bancoRepository,
                                    Service\Cartola $cartolaService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'movimientos' => []
        ];
        try {
            $inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
            $banco = $bancoRepository->fetchById($body['banco']);
            $mes = new DateTimeImmutable($body['mes']);
            $file = $request->getUploadedFiles()['file'];
            $output['movimientos'] = $cartolaService->process($inmobiliaria, $banco, $mes, $file);
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function exportar(ServerRequestInterface $request, ResponseInterface $response,
                                    Repository\Inmobiliaria $inmobiliariaRepository,
                                    Repository\Banco $bancoRepository,
                                    Service\Cartola $cartolaService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'filename' => ''
        ];
        try {
            $inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria']);
            $banco = $bancoRepository->fetchById($body['banco']);
            $mes = new DateTimeImmutable($body['mes']);
            $output['filename'] = $cartolaService->export($inmobiliaria, $banco, $mes, json_decode($body['movimientos']));
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function diaria(ServerRequestInterface $request, ResponseInterface $response,
                           Repository\Inmobiliaria\Cuenta $cuentaRepository,
                           Service\Cartola $cartolaService): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'cartolas' => []
        ];
        $fields = json_decode($body['fields']);
        foreach ($fields as $field) {
            try {
                $cuenta = $cuentaRepository->fetchById($body["cuenta_id{$field}"]);
                $fecha = new DateTimeImmutable($body["fecha{$field}"]);
                if (array_key_exists("manual{$field}", $body)) {
                    $file = json_decode($body["file{$field}"], JSON_OBJECT_AS_ARRAY);
                    $output['cartolas'][$field] = $cartolaService->diariaManual($cuenta, $fecha, $file);
                    continue;
                }
                $file = $request->getUploadedFiles()["file{$field}"];

                $output['cartolas'][$field] = $cartolaService->diaria($cuenta, $fecha, $file);
            } catch (EmptyResult $exception) {
                $this->logger->debug($exception);
            }
        }
        return $this->withJson($response, $output);
    }
    public function ayer(ServerRequestInterface $request, ResponseInterface $response,
                         Repository\Inmobiliaria\Cuenta $cuentaRepository,
                         Repository\Cartola $cartolaRepository): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'cartola' => []
        ];
        try {
            $cuenta = $cuentaRepository->fetchById($body['cuenta_id']);
            $fecha = new DateTimeImmutable($body['fecha']);
            $output['cartola'] = $cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $fecha);
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
