<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class AgentClickLog extends Model
{
    /**
     * 数据库连接名
     */
    protected $connection = 'mysql';

    /**
     * 与模型关联的表名
     */
    protected $table = 'webgame.agent_click_logs';

    /**
     * 可批量赋值的属性
     */
    protected $fillable = [
        'code',
        'country',
        'city',
        'user_agent',
        'client_ip',
        'referrer',
        'device_type',
        'is_valid'
    ];

    /**
     * 指定不需要更新时间戳
     */
    const UPDATED_AT = null;

    /**
     * 根据User-Agent判断设备类型
     *
     * @param string $userAgent
     * @return string
     */
    public static function detectDeviceType($userAgent)
    {
        $userAgent = strtolower($userAgent);

        if (strpos($userAgent, 'mobile') !== false ||
            strpos($userAgent, 'android') !== false ||
            strpos($userAgent, 'iphone') !== false) {
            return 'mobile';
        } elseif (strpos($userAgent, 'ipad') !== false ||
                 strpos($userAgent, 'tablet') !== false) {
            return 'tablet';
        } else {
            return 'desktop';
        }
    }

    /**
     * 获取关联的邀请码信息
     */
    public function agentInfo()
    {
        return $this->belongsTo(AgentUserInfo::class, 'code', 'referral_code');
    }
}
