<?php
namespace Incoviba\Service\Venta;

use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Exception\ServiceAction\{Read, Create, Update};
use Incoviba\Model;
use Incoviba\Repository;
use Incoviba\Service;

class BonoPie extends Ideal\Service
{
    public function __construct(LoggerInterface $logger,
                                protected Repository\Venta\BonoPie $bonoPieRepository,
                                protected Service\Valor $valorService,
                                protected Service\UF $ufService,
                                protected Pago $pagoService)
    {
        parent::__construct($logger);
    }

    /**
     * @param array $data
     * @return Model\Venta\BonoPie
     * @throws Create
     */
    public function add(array $data): Model\Venta\BonoPie
    {
        if (array_key_exists('valor', $data)) {
            $data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
        }
        if (!array_key_exists('pago', $data)) {
            $pago = $this->pagoService->add($data);
            $data['pago'] = $pago->id;
        }
        $filteredData = $this->bonoPieRepository->filterData($data);
        try {
            return $this->bonoPieRepository->fetchByPago($filteredData['pago']);
        } catch (EmptyResult) {
            $filteredData['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
            $bono = $this->bonoPieRepository->create($filteredData);
            try {
                return $this->bonoPieRepository->save($bono);
            } catch (PDOException $exception) {
                throw new Create(__CLASS__, $exception);
            }
        }
    }

    /**
     * @param Model\Venta\BonoPie $bonoPie
     * @param array $data
     * @return Model\Venta\BonoPie
     * @throws Update
     */
    public function edit(Model\Venta\BonoPie $bonoPie, array $data): Model\Venta\BonoPie
    {
        if (array_key_exists('valor', $data)) {
            $data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true);
        }
        if (!array_key_exists('pago', $data)) {
            $pago = $this->pagoService->edit($bonoPie->pago, $data);
            $data['pago'] = $pago->id;
        }
        $data['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true);
        $filteredData = $this->bonoPieRepository->filterData($data);
        try {
            return $this->bonoPieRepository->edit($bonoPie, $filteredData);
        } catch (PDOException | EmptyResult $exception) {
            throw new Update(__CLASS__, $exception);
        }
    }

    /**
     * @param int $venta_id
     * @return Model\Venta\BonoPie
     * @throws Read
     */
    public function getByVenta(int $venta_id): Model\Venta\BonoPie
    {
        try {
            return $this->bonoPieRepository->fetchByVenta($venta_id);
        } catch (EmptyResult $exception) {
            throw new Read(__CLASS__, $exception);
        }
    }
}
