<?php

namespace App\Models;

use CodeIgniter\Model;

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

    /**
     * Fetch a template by code and variant, falling back to 'default' if needed.
     */
    public function getTemplate(string $code, string $variant = 'default'): ?array
    {
        // Try exact (active only)
        $row = $this->where('code', $code)
                    ->where('variant', $variant)
                    ->where('is_active', 1)
                    ->first();
        if ($row) {
            return $row;
        }

        // Fallback to default variant
        $row = $this->where('code', $code)
                    ->where('variant', 'default')
                    ->where('is_active', 1)
                    ->first();

        return $row ?: null;
    }




}
