<?php

namespace App\Models;

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

/**
 * PRC-619 — admin bildirim kanalları (slack/telegram/discord/whatsapp/
 * pushover) scoped görünümü. creating'de channel ATANMAZ — çağıran
 * (seeder/servis) channel'ı açıkça verir; geçersiz/boş channel
 * InvalidArgumentException fırlatır.
 */
class NotificationTemplate extends CommunicationTemplate
{
    protected $table = 'communication_templates';

    protected static function booted(): void
    {
        parent::booted();
        static::addGlobalScope('channel', fn (Builder $q) => $q->whereIn('channel', self::ADMIN_CHANNELS));
        static::creating(function (self $model) {
            if (! in_array($model->channel, self::ADMIN_CHANNELS, true)) {
                throw new \InvalidArgumentException(
                    "NotificationTemplate yalnız admin kanallarını kabul eder; verilen: " . var_export($model->channel, true)
                );
            }
        });
    }

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