<?php

namespace App\Services;

use App\Http\Traits\CommonTrait;
use App\Http\Traits\AgentTrait;
use App\Http\Traits\BoardTrait;
use App\Models\User;
use App\Models\LoginLog;
use App\Models\DTO\SearchData;

class UserService
{
	use CommonTrait;
	use AgentTrait;
    use BoardTrait;

    public User $userModel;
    public LoginLog $loginLogModel;

	public function __construct()
    {
        $this->userModel = new User();
        $this->loginLogModel = new LoginLog();
	}

    /**
     * 로그인 기록 조회
     */
    public function loginLog(int $userID, SearchData $params): object
    {
        $loginLogs = $this->loginLogModel->logs($userID, $params);

        if ($loginLogs->rows > 0) {
            $num = listNum($loginLogs->total, $params->page, $params->perPage);
            foreach ($loginLogs->list as $i => $row) {
                $row->num = $num--;
                $row->os = $this->platform($row->user_agent);
                $row->browser = $this->browser($row->user_agent);
                $row->device = $this->device($row->user_agent);
                $row->result = ($row->success ? '성공' : '실패');

                $loginLogs->list[$i] = $row;
            }
        }

        return $loginLogs;
    }
}
