<?php

namespace App\Services\Sefaz;

use App\Models\Company;
use NFePHP\NFe\Tools;

/**
 * Monta a instância de Tools da sped-nfe para uma empresa.
 */
class SefazToolsFactory
{
    public function __construct(private readonly CertificateService $certificates) {}

    public function make(Company $company): Tools
    {
        $config = json_encode([
            'atualizacao' => now()->format('Y-m-d H:i:s'),
            'tpAmb' => (int) $company->ambiente,
            'razaosocial' => $company->razao_social,
            'cnpj' => $company->cnpj,
            'ie' => (string) $company->ie,
            'siglaUF' => $company->uf,
            'schemes' => 'PL_009_V4',
            'versao' => config('nfe.versao_nfe'),
            'tokenIBPT' => '',
            'CSC' => '',
            'CSCid' => '',
        ], JSON_THROW_ON_ERROR);

        $tools = new Tools($config, $this->certificates->load($company));
        $tools->model('55');

        return $tools;
    }
}
