<?php

namespace App\Models;

use App\Facades\CommonService;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LotteryWinMember extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', 'lottery_id', 'pic', 'name', 'mobile', 'award_id', 'code'];

    public function scopeAward($query, $award_id)
    {
        return $query->where("award_id", $award_id);
    }

    public function award()
    {
        return $this->belongsTo(LotteryAward::class, 'award_id', 'id');
    }

    protected function mobile(): Attribute
    {
        if (request()->routeIs('h5.*')) {
            return Attribute::make(
                get: fn ($value) => CommonService::hidePhone($value),
            );
        }
        return Attribute::make(
            get: fn ($value) => $value,
        );
    }

    public function scopeKeyword($query)
    {
        $keyword = request()->keyword;
        if ($keyword) {
            return $query->where(function ($sql) use($keyword){
                $sql->where('name', 'like', '%'.$keyword.'%')
                    ->orWhere('mobile', 'like', '%'.$keyword.'%');
            });
        }
        return $query;
    }

}
