<?php

declare(strict_types=1);

namespace app\admin\controller\user;

use app\admin\model\User;
use app\admin\model\UserScoreLog;
use app\common\controller\Backend;
use Webman\Http\Request;
use support\Response;

class ScoreLog extends Backend
{
    protected ?object $model = null;

    protected array $withJoinTable = ['user'];
    protected array|string $preExcludeFields = ['create_time'];
    protected array|string $quickSearchField = ['user.username', 'user.nickname'];

    protected function initController(Request $request): ?Response
    {
        $this->model = new UserScoreLog();
        return null;
    }

    public function add(Request $request): Response
    {
        $response = $this->initializeBackend($request);
        if ($response !== null) return $response;

        if ($request->method() === 'POST') {
            return $this->_add();
        }

        $userId = $request->get('userId', $request->post('userId', 0));
        $user = User::where('id', $userId)->find();
        if (!$user) {
            return $this->error(__("The user can't find it"));
        }
        return $this->success('', ['user' => $user]);
    }
}
