<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MessageTemplate extends Model
{
    protected $guarded = ['id'];
    protected $casts = ['active' => 'boolean'];

    public function bodyFor(string $language): string
    {
        return $this->{'body_'.substr($language, 0, 2)} ?: ($this->body_fa ?: ($this->body_en ?: ''));
    }

    /** Persian, then Pashto, then English — stacked into one message. */
    public function bodyAll(): string
    {
        return collect([$this->body_fa, $this->body_ps, $this->body_en])
            ->filter(fn ($b) => filled($b))
            ->implode("\n\n━━━━━━\n\n");
    }

    /**
     * The body to actually send: tri-lingual (fa+ps+en) when enabled in the
     * control room, otherwise the single default-language body.
     */
    public function composed(): string
    {
        if (\App\Support\SystemConfig::get('whatsapp_trilingual', true)) {
            $all = $this->bodyAll();
            if ($all !== '') {
                return $all;
            }
        }
        $lang = \App\Models\CrmSetting::current()->default_language ?: 'fa';

        return $this->bodyFor($lang);
    }
}
