<?php

namespace App\Models;

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

class RegularActivity extends BaseModel
{
    use HasFactory;

    public $fillable = ['title', 'pic', 'date', 'background', "detail_qrcode",  'sign_qrcode', "sign_qrcode2", 'start_time', 'end_time', 'status','is_provide_food','is_need_hand'];

    public const ACTIVE_STATUS = "active";
    public const ONGOING_STATUS = "ongoing";
    public const DONE_STATUS = "done";

    public const ACTIVITY_ID = 31;
    public const ALPHA_ACTIVITY_ID = 1;

    public const DEFAULT_PIC = "https://images.health.ufutx.com/202404/01/cba001fb1709181a524a185183c4d038.jpeg";

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

    public function orders()
    {
        return $this->hasMany(RegularActivityOrder::class, 'activity_id', 'id');
    }


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

    public function scopeDate($query)
    {
        $date = request()->date;
        if ($date) return $query->where("date", 'like', $date);
        return $query;
    }

    public function scopeValid($query)
    {
        $now = date("Y-m-d H:i:s");
        return $query->where('end_time', '>', $now)->whereNotNull("pic");
    }

    public function lottery()
    {
        return $this->morphOne(Lottery::class, 'lotteryable');
    }

    public function activityQrcodes()
    {
        return $this->hasMany(RegularActivityQrcode::class, 'activity_id', 'id');
    }

    public function membersLogs()
    {
        return $this->hasMany(RegularActivityMemberLog::class, 'activity_id', 'id');
    }
}
