<?php
namespace Incoviba\Service\Contabilidad\Informe\Tesoreria;

use Psr\Log\LoggerInterface;
use Psr\Http\Message\UploadedFileInterface;
use Incoviba\Common\Ideal;

class Input extends Ideal\Service
{
    public function __construct(LoggerInterface $logger,
                                protected Input\Excel $excelService,
                                protected Input\Data\SaldosContables $saldosContablesService,
                                protected Input\Data\DAPyFFMM $dapYFFMMService)
    {
        parent::__construct($logger);
    }

    public function loadFromExcel(UploadedFileInterface $uploadedFile): array
    {
        if ($uploadedFile->getSize() === 0) {
            return [];
        }
        $tmpFile = '/tmp/' . $uploadedFile->getClientFilename();
        $uploadedFile->moveTo($tmpFile);
        $data = $this->excelService->load($tmpFile);
        unlink($tmpFile);

        return $data;
    }

    public function load(array $data): void
    {
        $this->saldosContablesService->load($data['saldosContables']);
        $this->dapYFFMMService->load($data['dapyffmm']);
    }
}
