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

use Psr\Log\LoggerInterface;
use PhpOffice\PhpSpreadsheet;
use Incoviba\Common\Ideal;

class Excel extends Ideal\Service
{
    public function __construct(LoggerInterface $logger,
                                protected Excel\SaldosCuentas $saldosCuentasService,
                                protected Excel\DAPyFFMM $dapyFFMMService,
                                protected Excel\DeudaBanco $deudaBancoService)
    {
        parent::__construct($logger);
    }

    public function load(string $filename): array
    {
        try {
            $reader = PhpSpreadsheet\IOFactory::createReaderForFile($filename);
        } catch (PhpSpreadsheet\Reader\Exception $e) {
            $this->logger->error($e->getMessage());
            return [];
        }
        $excel = $reader->load($filename);

        $data = [];
        $data['saldosContables'] = $this->saldosCuentasService->load($excel);
        $data['dapyffmm'] = $this->dapyFFMMService->load($excel);
        $data['deudaBanco'] = $this->deudaBancoService->load($excel);

        return $data;
    }
}
