<?php

declare(strict_types=1);

namespace Tricks\LaravelTools\MJML;

use Spatie\Mjml\Mjml as BaseMjml;

final class Mjml
{
    public static function convert(string $input, ?string $output = null): bool
    {
        $mjml = file_get_contents($input);
        if ($mjml === false) {
            return false;
        }

        $html = BaseMjml::new()
            ->minify()
            ->toHtml($mjml);

        $output ??= str_replace('_mjml', '', $input);
        $success = file_put_contents($output, $html);

        return $success !== false;
    }

    public static function convertTemplate(): void
    {
        Mjml::convert(
            __DIR__.'/../resources/views/emails/layout_mjml.blade.php',
        );
    }
}
