<?php

namespace App\Models;

use App\Enums\WithdrawalRequestStatus;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

#[Fillable([
    'investment_id',
    'user_id',
    'amount',
    'fee_total',
    'net_amount',
    'requester_note',
    'destination_currency_network',
    'destination_address',
    'destination_label',
    'status',
    'admin_note',
    'payout_txid',
    'processed_by',
    'processed_at',
])]
class WithdrawalRequest extends Model
{
    /**
     * @return BelongsTo<Investment, WithdrawalRequest>
     */
    public function investment(): BelongsTo
    {
        return $this->belongsTo(Investment::class);
    }

    /**
     * @return BelongsTo<User, WithdrawalRequest>
     */
    public function requester(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    /**
     * @return BelongsTo<User, WithdrawalRequest>
     */
    public function processor(): BelongsTo
    {
        return $this->belongsTo(User::class, 'processed_by');
    }

    /**
     * @return HasMany<InvestmentTransaction, WithdrawalRequest>
     */
    public function investmentTransactions(): HasMany
    {
        return $this->hasMany(InvestmentTransaction::class, 'withdrawal_request_id');
    }

    protected function casts(): array
    {
        return [
            'status' => WithdrawalRequestStatus::class,
            'amount' => 'decimal:2',
            'fee_total' => 'decimal:2',
            'net_amount' => 'decimal:2',
            'processed_at' => 'datetime',
        ];
    }
}
