<?php
namespace Incoviba\Controller\API\Ventas\Abonos;

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

class Cuotas extends Ideal\Controller
{
    use API\withJson;

    public function add(ServerRequestInterface $request, ResponseInterface $response,
                        Service\Venta\Pago $pagoService,
                        Service\UF $ufService,
                        Service\Valor $valorService,
                        Repository\Venta $ventaRepository,
                        Repository\Venta\Abono\Cuota $cuotaRepository): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'cuota' => null,
            'success' => false,
        ];
        try {
            $venta = $ventaRepository->fetchById($input['venta_id']);
            $input['valor'] = $valorService->clean($input['valor']);
            $uf = $venta->uf;
            $fecha = new DateTimeImmutable($input['fecha']);
            if ($fecha <= new DateTimeImmutable()) {
                $uf = $ufService->get($fecha);
            }
            $input['uf'] = $uf;
            $pagoData = array_intersect_key($input, array_flip(['fecha', 'valor', 'uf']));
            $pago = $pagoService->add($pagoData);
            $cuotaData = array_intersect_key($input, array_flip(['venta_id', 'numero']));
            $cuotaData['pago_id'] = $pago->id;
            $cuota = $cuotaRepository->create($cuotaData);
            $output['cuota'] = $cuotaRepository->save($cuota);
            $output['success'] = true;
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function edit(ServerRequestInterface $request, ResponseInterface $response,
                         Service\Venta\Pago $pagoService,
                         Repository\Venta\EstadoPago $estadoPagoRepository,
                         Service\UF $ufService,
                         Service\Valor $valorService,
                         Repository\Venta\Abono\Cuota $cuotaRepository, int $cuota_id): ResponseInterface
    {
        $input = $request->getParsedBody();
        $output = [
            'input' => $input,
            'cuota' => null,
            'success' => false,
        ];
        try {
            $cuota = $cuotaRepository->fetchById($cuota_id);
            $input['valor'] = $valorService->clean($input['valor']);
            if (isset($input['uf']) and !empty($input['uf'])) {
                $uf = $ufService->get(new DateTimeImmutable($input['fecha']));
                $input['valor'] = $uf * $valorService->clean($input['uf']);
            }
            $pagoData = array_intersect_key($input, array_flip(['fecha', 'valor']));
            $pago = $pagoService->edit($cuota->pago, $pagoData);
            if ($input['tipo_estado_id'] !== $pago->currentEstado->tipoEstadoPago->id) {
                $estadoData = array_intersect_key($input, array_flip(['fecha']));
                $estadoData['pago'] = $cuota->pago->id;
                $estadoData['estado'] = $input['tipo_estado_id'];
                $estado = $estadoPagoRepository->create($estadoData);
                $estadoPagoRepository->save($estado);
            }
            $output['cuota'] = $cuota;
            $output['success'] = true;
        } catch (Implement\Exception\EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
