<?php 

namespace App\Models;
use CodeIgniter\Model;

class EmailTemplateModel extends Model {
    protected $table = 'email_templates';
    protected $primaryKey = 'id';
    protected $allowedFields = ['code', 'variant', 'subject', 'body_html', 'is_active', 'updated_by', 'updated_at'];

    public function getActiveTemplates(): array {
        return $this->select('email_templates.*, code AS template_key, code AS name, body_html AS body')
            ->where('is_active', 1)
            ->orderBy('code', 'asc')
            ->findAll();
    }

    public function findByKey(string $key): ?array {
        return $this->select('email_templates.*, code AS template_key, code AS name, body_html AS body')
            ->where('code', $key)
            ->where('is_active', 1)
            ->first();
    }
}
