<?php

namespace App\Models;

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

class RegularActivityMember extends BaseModel
{
    use HasFactory;

    public $fillable = ['user_id', 'activity_id', 'status'];

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

    public function agentUser()
    {
        return $this->belongsTo(AgentUser::class, 'user_id', 'user_id');
    }

    public function scopeSuccess($query){
        return $query->where('status', 1);
    }

    public function scopeFailure($query) {
        return $query->where("status", 0);
    }

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