<?php

namespace App\Models;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class CommunicationUnitWallet extends Model
{
    use BelongsToTenant;

    public const CHANNEL_SMS = 'sms';
    public const CHANNEL_WHATSAPP = 'whatsapp';

    protected $fillable = [
        'tenant_id',
        'channel',
        'balance_units',
        'cost_balance',
        'total_credited_units',
        'total_credited_amount',
        'total_used_units',
        'total_used_cost',
        'total_member_charges',
    ];

    protected function casts(): array
    {
        return [
            'balance_units' => 'integer',
            'cost_balance' => 'decimal:2',
            'total_credited_units' => 'integer',
            'total_credited_amount' => 'decimal:2',
            'total_used_units' => 'integer',
            'total_used_cost' => 'decimal:2',
            'total_member_charges' => 'decimal:2',
        ];
    }

    public static function channels(): array
    {
        return [
            self::CHANNEL_SMS => 'SMS',
            self::CHANNEL_WHATSAPP => 'WhatsApp',
        ];
    }

    public function transactions(): HasMany
    {
        return $this->hasMany(CommunicationUnitTransaction::class);
    }

    public function channelLabel(): string
    {
        return self::channels()[$this->channel] ?? str($this->channel)->headline()->toString();
    }
}
