<?php
namespace Incoviba\Service;

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

class Informe extends Ideal\Service
{
    public function __construct(LoggerInterface $logger, protected string $folder)
    {
        parent::__construct($logger);
        if (!file_exists($this->folder)) {
            mkdir($this->folder);
        }
    }

    protected array $informes;

    public function register(string $name, string $informeClass): Informe
    {
        $this->informes[$name] = $informeClass;
        return $this;
    }

    public function build(string $type, string $filename, string $title, array $data): void
    {
        $informe = new $this->informes[$type]();
        $filename = implode(DIRECTORY_SEPARATOR, [$this->folder, "{$filename}.xlsx"]);
        $informe->setFilename($filename);
        $informe->setTitle($title);
        $informe->addData($data);
        $informe->build();
    }
}
