<?php

namespace App\Models;

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

class BonusTransaction extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'bonus_conversion_id',
        'trade_id',
        'transaction_type',
        'amount',
        'turnover_contribution',
        'description',
        'metadata',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'amount' => 'decimal:8',
        'turnover_contribution' => 'decimal:8',
        'metadata' => 'array',
    ];

    /**
     * Get the bonus conversion.
     */
    public function bonusConversion(): BelongsTo
    {
        return $this->belongsTo(BonusConversion::class);
    }

    /**
     * Get the related trade.
     */
    public function trade(): BelongsTo
    {
        return $this->belongsTo(Trade::class);
    }

    /**
     * Scope to filter by transaction type.
     */
    public function scopeOfType($query, string $type)
    {
        return $query->where('transaction_type', $type);
    }

    /**
     * Scope to filter by bonus conversion.
     */
    public function scopeForConversion($query, int $conversionId)
    {
        return $query->where('bonus_conversion_id', $conversionId);
    }
}

// Made with Bob