<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property int $id
 * @property int $payment_id
 * @property int $provider_id
 * @property string $gateway_provider
 * @property string|null $gateway_transfer_target_reference
 * @property string|null $gateway_transfer_target_label
 * @property \App\Enums\PaymentSplitStatusEnum $status
 * @property numeric $gross_amount Valor bruto destinado ao repasse do provider.
 * @property numeric $gateway_fee_amount Custo do gateway associado ao repasse.
 * @property numeric $net_amount Valor liquido do repasse: net_amount = gross_amount - gateway_fee_amount.
 * @property \Illuminate\Support\Carbon|null $transferred_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 int|null $provider_withdrawal_id
 * @property-read \App\Models\Payment $payment
 * @property-read \App\Models\Provider $provider
 * @property-read \App\Models\ProviderWithdrawal|null $providerWithdrawal
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereGatewayFeeAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereGatewayProvider($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereGatewayTransferTargetLabel($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereGatewayTransferTargetReference($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereGrossAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereMetadata($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereNetAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit wherePaymentId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereProviderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereProviderWithdrawalId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereTransferredAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit withTrashed(bool $withTrashed = true)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|PaymentSplit withoutTrashed()
 * @mixin \Eloquent
 */
class PaymentSplit extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'payment_splits';

    protected $fillable = [
        'payment_id',
        'provider_id',
        'provider_withdrawal_id',
        'gateway_provider',
        'gateway_transfer_target_reference',
        'gateway_transfer_target_label',
        'status',
        'gross_amount',
        'gateway_fee_amount',
        'net_amount',
        'transferred_at',
        'metadata',
    ];

    protected $casts = [
        'gross_amount'       => 'decimal:2',
        'gateway_fee_amount' => 'decimal:2',
        'net_amount'         => 'decimal:2',
        'status'             => \App\Enums\PaymentSplitStatusEnum::class,
        'transferred_at'     => 'datetime',
        'metadata'           => 'array',
        'created_at'         => 'datetime',
        'updated_at'         => 'datetime',
        'deleted_at'         => 'datetime',
    ];

    public function payment()
    {
        return $this->belongsTo(Payment::class);
    }

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

    public function providerWithdrawal()
    {
        return $this->belongsTo(ProviderWithdrawal::class);
    }
}
