<?php

namespace App\Models;

use CodeIgniter\Model;

class DeliveryNoteTemplateModel extends Model
{
    protected $table = 'delivery_note_templates';
    protected $primaryKey = 'id';
    protected $returnType = 'array';
    protected $useAutoIncrement = true;
    protected $protectFields = true;
    protected $allowedFields = ['code', 'name', 'body_html', 'fields_json', 'is_active'];
    protected $useTimestamps = true;

    public function fields(array $template): array
    {
        $fields = json_decode((string) ($template['fields_json'] ?? '[]'), true);
        return is_array($fields) ? $fields : [];
    }
}
