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

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

class Pagos
{
    use withJson;

    public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Pago $pagoRepository, int $pago_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'input' => $body,
            'pago_id' => $pago_id,
            'pago' => null
        ];
        try {
            $pago = $pagoRepository->fetchById($pago_id);
            if (isset($body['fecha'])) {
                $fecha = new DateTimeImmutable($body['fecha']);
                $body['fecha'] = $fecha->format('Y-m-d');
            }
            $output['pago'] = $pagoRepository->edit($pago, $body);
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function depositar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'pago_id' => $pago_id,
            'input' => $body,
            'depositado' => false
        ];
        try {
            $pago = $pagoService->getById($pago_id);
            $fecha = new DateTimeImmutable($body->fecha);
            $output['depositado'] = $pagoService->depositar($pago, $fecha);
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'pago_id' => $pago_id,
            'input' => $body,
            'abonado' => false
        ];
        try {
            $pago = $pagoService->getById($pago_id);
            $fecha = new DateTimeImmutable($body->fecha);
            $output['abonado'] = $pagoService->abonar($pago, $fecha);
            $output['input']['fecha'] = $fecha->format('d-m-Y');
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function devolver(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'pago_id' => $pago_id,
            'input' => $body,
            'devuelto' => false
        ];
        try {
            $pago = $pagoService->getById($pago_id);
            $fecha = new DateTimeImmutable($body->fecha);
            $output['devuelto'] = $pagoService->devolver($pago, $fecha);
            $output['input']['fecha'] = $fecha->format('d-m-Y');
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getPendientes();
        $pagos_pendientes = [];
        foreach ($pagos as $pago) {
            $pagos_pendientes []= [
                'id' => $pago->id
            ];
        }
        return $this->withJson($response, ['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]);
    }
    public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getDepositados();
        $pagos_depositados = [];
        foreach ($pagos as $pago) {
            $pagos_depositados []= [
                'id' => $pago->id
            ];
        }
        return $this->withJson($response, ['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]);
    }
    public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
    {
        $pagos = $pagoService->getRebotes();
        $rebotes = [];
        foreach ($pagos as $pago) {
            $rebotes []= [
                'id' => $pago->id
            ];
        }
        return $this->withJson($response, ['pagos' => $rebotes, 'total' => count($rebotes)]);
    }

    public function anular(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
    {
        $fecha = new DateTimeImmutable();
        $output = [
            'pago_id' => $pago_id,
            'fecha' => $fecha->format('d-m-Y'),
            'anulado' => false
        ];
        try {
            $pago = $pagoService->getById($pago_id);
            $output['anulado'] = $pagoService->anular($pago, $fecha);
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
