<?php

namespace App\Services;

use App\DTOs\Admin\TemplateSectionDto;
use App\Models\TemplateSection;
use Exception;
use Illuminate\Support\Facades\Log;

class TemplateSectionService
{
    public function find($id): ?TemplateSectionDto
    {
        try {
            $data = TemplateSection::findOrFail($id);
            return new TemplateSectionDto([
                "id"=> $data->id,
                'title' => $data->title,
                'order' => $data->order,
                "sectionRules" => $data->sectionRules->toArray(),
                "templateName"=> $data->template->name,
            ]);
        } catch (Exception $e) {
            Log::error('No se pudo encontrar la sección de la plantilla: ' . $e->getMessage(), [
                'data' => $id,
                'trace' => $e->getTraceAsString(),
            ]);
            return null;
        }
    }   
}