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

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

class Depositos extends Ideal\Controller
{
    use withJson;

    public function inmobiliaria(ServerRequestInterface           $request, ResponseInterface $response,
                                 Repository\Inmobiliaria          $inmobiliariaRepository,
                                 Repository\Contabilidad\Banco    $bancoRepository,
                                 Repository\Inmobiliaria\Cuenta   $cuentaRepository,
                                 Repository\Contabilidad\Deposito $dapRepository,
                                 int                              $inmobiliaria_rut): ResponseInterface
    {
        $output = [
            'inmobiliaria_rut' => $inmobiliaria_rut,
            'depositos' => []
        ];
        try {
            $inmobiliaria = $inmobiliariaRepository->fetchById($inmobiliaria_rut);
            $cuentas = $cuentaRepository->fetchByInmobiliaria($inmobiliaria->rut);
            foreach ($cuentas as $cuenta) {
                $output['depositos'] = $dapRepository->fetchByCuenta($cuenta->id);
            }
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function add(ServerRequestInterface           $request, ResponseInterface $response,
                        Repository\Inmobiliaria          $inmobiliariaRepository, Repository\Contabilidad\Banco $bancoRepository,
                        Repository\Inmobiliaria\Cuenta   $cuentaRepository,
                        Repository\Contabilidad\Deposito $dapRepository): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'status' => false
        ];
        try {
            $inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria_rut']);
            $banco = $bancoRepository->fetchById($body['banco_id']);
            $cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
            $data = [
                'id' => $body['id'],
                'cuenta_id' => $cuenta->id,
                'capital' => $body['capital'],
                'futuro' => $body['futuro'],
                'inicio' => (new DateTimeImmutable($body['inicio']))->format('Y-m-d'),
                'termino' => (new DateTimeImmutable($body['termino']))->format('Y-m-d')
            ];
            try {
                $dapRepository->fetchById($body['id']);
            } catch (Implement\Exception\EmptyResult) {
                $dap = $dapRepository->create($data);
                $dapRepository->save($dap);
            }
            $output['status'] = true;
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
