<?php

namespace App\Models;

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

class CommunicationUnitTopup extends Model
{
    use BelongsToTenant;

    public const STATUS_PENDING = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';

    protected $fillable = [
        'tenant_id',
        'created_by',
        'approved_by',
        'channel',
        'units',
        'unit_price',
        'amount',
        'payment_channel',
        'status',
        'reference',
        'gateway_reference',
        'proof_path',
        'notes',
        'rejected_reason',
        'approved_at',
        'processed_at',
    ];

    protected function casts(): array
    {
        return [
            'units' => 'integer',
            'unit_price' => 'decimal:2',
            'amount' => 'decimal:2',
            'approved_at' => 'datetime',
            'processed_at' => 'datetime',
        ];
    }

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

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

    public function transaction(): HasOne
    {
        return $this->hasOne(CommunicationUnitTransaction::class);
    }

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