<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * PRC-619 — communication_templates üzerinde channel='email' scoped görünüm.
 * STI-light KURALLAR (docs/PATTERNS_AND_GOTCHAS.md):
 *  - Bu sınıfla yapılan her sorgu OTOMATİK channel='email' filtrelenir.
 *  - create()/firstOrCreate() channel'ı otomatik atar; elle channel VERME.
 *  - is_html verilmezse true atanır (e-posta varsayılanı).
 *  - Cross-channel sorgu gerekiyorsa CommunicationTemplate kullan.
 */
class EmailTemplate extends CommunicationTemplate
{
    protected $table = 'communication_templates';

    protected static function booted(): void
    {
        parent::booted();
        static::addGlobalScope('channel', fn (Builder $q) => $q->where('channel', 'email'));
        static::creating(function (self $model) {
            $model->channel = 'email';
            $model->is_html ??= true;
        });
    }

    public function contents(): HasMany
    {
        return $this->hasMany(EmailTemplateContent::class, 'template_id');
    }
}
