<?php

namespace App\Models;

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

class Lottery extends BaseModel
{
    use HasFactory;

    public const REGULAR_ACTIVITY_TYPE = "regular_activity";
    public const ACTIVITY_TYPE = "activity";

    public $fillable = ['title', 'qrcode', 'path', 'repeat', 'apply_qrcode', 'lotteryable_type', 'lotteryable_id', 'new_qrcode', 'new_path'];

    public function awards()
    {
        return $this->hasMany(LotteryAward::class, 'lottery_id', 'id');
    }

    public function members()
    {
        return $this->hasMany(LotteryMember::class, 'lottery_id', 'id');
    }

    public function winMembers()
    {
        return $this->hasMany(LotteryWinMember::class, 'lottery_id', 'id');
    }

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->where('title', 'like', '%'.$keyword.'%');
        }
        return $query;
    }

    public function addLotteryable($able)
    {
        $this->update(['lotteryable_type'=>$able::class, 'lotteryable_id'=>$able->id]);
    }

    public function lotteryable()
    {
        return $this->morphTo();
    }
}
