<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;

class Bet extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id', 'reference', 'stake', 'total_odds',
        'potential_payout', 'actual_payout', 'status', 'type', 'settled_at',
    ];

    protected function casts(): array
    {
        return [
            'stake' => 'decimal:2',
            'total_odds' => 'decimal:4',
            'potential_payout' => 'decimal:2',
            'actual_payout' => 'decimal:2',
            'settled_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (Bet $bet) {
            $bet->reference = 'BET-' . strtoupper(Str::random(10));
        });
    }

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

    public function items()
    {
        return $this->hasMany(BetItem::class);
    }
}
