<?php

namespace App\Http\Controllers\Api\V1\Client\games;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\Client\Games\CheckHexlingGameRequest;
use App\Http\Requests\Api\V1\Client\Games\GetHexlingGameRequest;
use App\Http\Requests\Api\V1\Client\Games\GetHexlingGameResultRequest;
use App\Http\Requests\Api\V1\Client\Games\GetHexlingSubmissionsRequest;
use App\Services\Games\HexlingGameService;

class HexlingGameController extends Controller
{
    use \App\Traits\Formatter;

    protected $hexlingGameService;

    public function __construct(HexlingGameService $hexlingGameService)
    {
        $this->hexlingGameService = $hexlingGameService;
    }

    /**
     * Handle word checking logic for Hexling
     */
    public function checkHexlingGame(CheckHexlingGameRequest $request)
    {
        $validated = $request->validated();
        $user = auth('client')->user();

        $result = $this->hexlingGameService->checkWord($user, $validated);

        if (! $result['success']) {
            return response()->json([
                'success' => $result['success'],
                'message' => $result['message'],
            ], $result['status_code'] ?? 400);
        }

        return $this->withSuccess($result['data']);
    }

    public function getHexlingGame(GetHexlingGameRequest $request)
    {
        $validated = $request->validated();
        $user = auth('client')->user();

        $data = $this->hexlingGameService->getGamesList($user, $validated);

        return $this->withSuccess($data);
    }

    public function getHexlingSubmissions(GetHexlingSubmissionsRequest $request)
    {
        $validated = $request->validated();
        $user = auth('client')->user();

        $data = $this->hexlingGameService->getSubmissions($user, $validated);

        return response()->json([
            'success' => true,
            'data' => $data,
        ]);
    }

    /**
     * Get Hexling game result with comprehensive statistics
     */
    public function getHexlingGameResult(GetHexlingGameResultRequest $request)
    {
        $validated = $request->validated();
        $user = auth('client')->user();

        $data = $this->hexlingGameService->getGameResult($user, $validated);

        return $this->withSuccess($data);
    }
}
