<?php

namespace App\Models;

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

/**
 * @property int $id
 * @property int $provider_id
 * @property string $recipient_id
 * @property string|null $transfer_id
 * @property string $idempotency_key
 * @property numeric $gross_amount
 * @property numeric $gateway_fee_amount
 * @property numeric $net_amount
 * @property \App\Enums\ProviderWithdrawalStatusEnum $status
 * @property string|null $type
 * @property array<array-key, mixed>|null $bank_account
 * @property string|null $bank_response
 * @property array<array-key, mixed>|null $gateway_payload
 * @property \Illuminate\Support\Carbon|null $completed_at
 * @property \Illuminate\Support\Carbon|null $failed_at
 * @property array<array-key, mixed>|null $metadata
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property \Illuminate\Support\Carbon|null $deleted_at
 * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\PaymentSplit> $paymentSplits
 * @property-read int|null $payment_splits_count
 * @property-read \App\Models\Provider $provider
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereBankAccount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereBankResponse($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereCompletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereFailedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereGatewayFeeAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereGatewayPayload($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereGrossAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereIdempotencyKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereMetadata($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereNetAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereProviderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereRecipientId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereTransferId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal withTrashed(bool $withTrashed = true)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|ProviderWithdrawal withoutTrashed()
 * @mixin \Eloquent
 */
class ProviderWithdrawal extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'provider_withdrawals';

    protected $fillable = [
        'provider_id',
        'recipient_id',
        'transfer_id',
        'idempotency_key',
        'gross_amount',
        'gateway_fee_amount',
        'net_amount',
        'status',
        'type',
        'bank_account',
        'bank_response',
        'gateway_payload',
        'completed_at',
        'failed_at',
        'metadata',
    ];

    protected function casts(): array
    {
        return [
            'gross_amount'       => 'decimal:2',
            'gateway_fee_amount' => 'decimal:2',
            'net_amount'         => 'decimal:2',
            'status'             => \App\Enums\ProviderWithdrawalStatusEnum::class,
            'bank_account'       => 'array',
            'gateway_payload'    => 'array',
            'metadata'           => 'array',
            'completed_at'       => 'datetime',
            'failed_at'          => 'datetime',
        ];
    }

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

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