<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Model;

class Withdrawal extends Model
{
    use HasFactory;

    /**
     * @var array<int, string>
     */
    protected $fillable = [
        'user_id',
        'request_number',
        'amount',
        'tds_amount',
        'charge_amount',
        'net_amount',
        'locked_amount',
        'hold_until',
        'status',
        'requested_at',
        'processed_at',
        'processed_by',
        'rejection_reason',
        'payment_reference',
        'payment_proof_path',
    ];

    /**
     * @var array<string, string>
     */
    protected $casts = [
        'amount' => 'decimal:2',
        'tds_amount' => 'decimal:2',
        'charge_amount' => 'decimal:2',
        'net_amount' => 'decimal:2',
        'locked_amount' => 'decimal:2',
        'hold_until' => 'date',
        'requested_at' => 'datetime',
        'processed_at' => 'datetime',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

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

    public function latestPayout(): HasOne
    {
        return $this->hasOne(PayoutTransaction::class)->latestOfMany();
    }
}
