<?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 Escrituras
{
    use withJson;

    public function add(ServerRequestInterface $request, ResponseInterface $response,
                        Service\Venta\Escritura $escrituraService, int $venta_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'venta_id' => $venta_id,
            'input' => $body,
            'escritura' => null,
            'status' => false
        ];
        try {
            $output['escritura'] = $escrituraService->add($venta_id, $body);
            $output['status'] = true;
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
    public function edit(ServerRequestInterface $request, ResponseInterface $response,
                         Service\Venta\Escritura $escrituraService,
                         int                    $venta_id): ResponseInterface
    {
        $body = $request->getParsedBody();
        $output = [
            'venta_id' => $venta_id,
            'input' => $body,
            'success' => false
        ];
        try {
            $escrituraService->edit($venta_id, $body);
            $output['success'] = true;
        } catch (EmptyResult) {}
        return $this->withJson($response, $output);
    }
}
