<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class AccessLog extends Model
{
    protected $table = 'tb_access_log';
    protected $primaryKey = 'id';

    public $keyType = 'int';
    public $incrementing = false;
    public $timestamps = false;

    const CREATED_AT = 'created_at';
    const UPDATED_AT = null;
    const DELETED_AT = null;

    protected $guarded = [];

    /**
     * DB 접근 기록 등록
     */
    public function register(Request $request): bool
    {
        return $this->insert([
            'request_uri' => $request->getMethod(),
            'script' => $request->getScriptName(),
            'method' => $request->getMethod(),
            'locale' => $request->getLocale(),
            'client_ip' => IP_ADDRESS,
            'referer' => REFERER,
            'user_agent' => USER_AGENT,
            'browser' => BROWSER,
            'os' => PLATFORM,
            'device' => DEVICE,
            'encodings' => join(', ', $request->getEncodings()),
            'format' => $request->getContentTypeFormat(),
            'created_at' => now()
        ]);
    }
}
