<?php

namespace App\Models;

use App\Models\Concerns\BelongsToBranch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CommunicationLog extends Model
{
    use BelongsToBranch;

    protected $fillable = [
        'branch_id', 'channel', 'recipient_type', 'recipient_id', 'recipient_name',
        'to', 'subject', 'body', 'status', 'error', 'sent_by', 'sent_at',
    ];

    protected function casts(): array
    {
        return ['sent_at' => 'datetime'];
    }

    public const CHANNELS = ['sms' => 'SMS', 'whatsapp' => 'WhatsApp', 'email' => 'Email'];

    public function sender(): BelongsTo
    {
        return $this->belongsTo(User::class, 'sent_by');
    }

    public function getChannelColorAttribute(): string
    {
        return ['sms' => 'primary', 'whatsapp' => 'success', 'email' => 'info'][$this->channel] ?? 'secondary';
    }

    public function getStatusColorAttribute(): string
    {
        return ['sent' => 'success', 'queued' => 'warning', 'failed' => 'danger'][$this->status] ?? 'secondary';
    }
}
