<?php

namespace App\Http\Controllers\admin;

use App\Constants\HttpStatus;
use App\Http\Controllers\Controller;
use App\Services\GameService;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Exception;
use App\Models\Game as GameModel;

class Game extends Controller
{

    /**
     * @api {get} /admin/game 游戏列表
     * @apiGroup 游戏管理
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} [page=1]
     * @apiParam {int} [limit=10]
     * @apiParam {string} [game_name] 游戏名称
     *
     * @apiSuccess (data) {Object} data
     * @apiSuccess (data) {int} data.total 数量
     * @apiSuccess (data) {Object[]} data.data 列表
     * @apiSuccess (data) {int} data.data.id
     * @apiSuccess (data) {int} data.data.game_name 游戏名称
     * @apiSuccess (data) {string} data.data.updated_at
     * @apiSuccess (data) {string} data.data.created_at
     */
    function index()
    {
        try {
            request()->validate([
                'game_name' => ['nullable', 'string'],
            ]);
            $search = request()->all();
            $result = GameService::paginate($search);
        } catch (ValidationException $e) {
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            return $this->error(intval($e->getCode()));
        }
        return $this->success($result);
    }

    /**
     * @api {post} /admin/game/update 修改
     * @apiDescription 更新或者新增都用这个接口
     * @apiGroup 游戏管理
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} id 要更新的ID
     * - 如果是新增，则id=0
     * @apiParam {string} game_name 游戏名称
     */
    public function update()
    {
        DB::beginTransaction();
        try {
            request()->validate([
                'id' => ['required', 'integer', 'min:0', 'max:99999999'],
                'game_name' => ['required', 'string', 'min:1', 'max:32'],
            ]);
            $id = request()->input('id', 0);
            $gameName = request()->input('game_name');
            $gameName = trim($gameName);
            if (GameModel::where('game_name', $gameName)->first()) {
                throw new Exception('游戏名称已存在', HttpStatus::CUSTOM_ERROR);
            }


            GameModel::updateOrCreate(
                ['id' => $id],
                ['game_name' => $gameName]
            );
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error(intval($e->getCode()), $e->getMessage());
        }
        return $this->success();
    }


    /**
     * @api {post} /admin/game/delete 删除
     * @apiGroup 游戏管理
     * @apiUse result
     * @apiUse header
     * @apiVersion 1.0.0
     *
     * @apiParam {int} id 要删除的ID
     *
     */
    function destroy()
    {
        DB::beginTransaction();
        try {
            request()->validate([
                'id' => ['required', 'integer', 'min:1', 'max:99999999'],
            ]);
            $id = request()->input('id', 0);
            $game = GameModel::where('id', $id)->first();
            if (!$game) throw new Exception("数据不存在", HttpStatus::CUSTOM_ERROR);
            $game->delete();
            DB::commit();
        } catch (ValidationException $e) {
            DB::rollBack();
            return $this->error(HttpStatus::VALIDATION_FAILED, $e->validator->errors()->first());
        } catch (Exception $e) {
            DB::rollBack();
            return $this->error(intval($e->getCode()));
        }
        return $this->success();
    }


}