<?php

namespace App\Models;

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

class LottoCode extends BaseModel
{
    use HasFactory, SoftDeletes;

    public $fillable = ['user_id', 'order_type', 'order_id', 'code', 'member_id', 'award'];

    protected $casts = [
        'award' => 'array',
    ];

    public function scopeUser($query, $user_id=null)
    {
        if ($user_id) {
            return $query->where('user_id', $user_id);
        }
        return $query;
    }

    public function order()
    {
        return $this->belongsTo(AgentOrder::class, 'order_id', 'id');
    }

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

    public function winMember()
    {
        return $this->hasOne(LotteryWinMember::class, 'code', 'code');
    }
}
