<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Activity extends BaseModel
{
    use HasFactory, SoftDeletes;
    protected $table = 'activity';

    const STATUS_CLOSE = 0;
    const STATUS_OPEN = 1;

    const FREE = 0;
    const PAY = 1;

    const ONLINE = 0;
    const OFFLINE = 1;

    public const SIGN_FEE = 200;

    //活动id
    public const ACTIVITY_ID = 27;
    public const ALPHA_ACTIVITY_ID = 1;


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

    public function scopeValid($query)
    {
        return $query->show()->time();
    }

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

    /**
     * 获取 sku 字段的数组形式
     *
     * @param  string $value
     * @return array
     */
    protected function sku(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => json_decode($value,true),
        );
    }

    public function payment()
    {
        return $this->belongsTo(Payment::class, 'payment_id', 'id');
    }

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

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

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