<?php

namespace App\Http\Controllers;

use App\Models\Competition;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class CompetitionController extends Controller
{
    public static function getPublics(int $skip = 0, int $take = 10): JsonResponse
    {
        if ($skip < 0) {
            $skip = 0;
        }
        if ($take < 1 || $take > 100) {
            $take = 10;
        }

        $competitions = Competition::where('status', 'public')
            ->orderBy('start_date', 'desc')
            ->skip($skip)
            ->take($take)
            ->get();

        $total = Competition::where('status', 'public')->count();

        return response()->json([
            'data' => $competitions,
            'meta' => [
                'skip' => $skip,
                'take' => $take,
                'total' => $total,
                'hasMore' => ($skip + $take) < $total
            ]
        ]);
    }

    public static function getUser(User $user, int $skip = 0, int $take = 10): JsonResponse
    {
        if ($skip < 0) {
            $skip = 0;
        }
        if ($take < 1 || $take > 100) {
            $take = 10;
        }

        $query = Competition::where('owner', $user->id);

        $competitions = $query->orderBy('start_date', 'desc')
            ->skip($skip)
            ->take($take)
            ->get();

        $total = $query->count();

        return response()->json([
            'data' => $competitions,
            'meta' => [
                'skip' => $skip,
                'take' => $take,
                'total' => $total,
                'hasMore' => ($skip + $take) < $total
            ]
        ]);
    }

    /**
     * Get all public competitions/tournaments with pagination.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getPublicCompetitions(Request $request): JsonResponse
    {
        $request->validate([
            'skip' => 'integer|min:0',
            'take' => 'integer|min:1|max:100',
        ]);

        $skip = $request->input('skip', 0);
        $take = $request->input('take', 10);

        $competitions = Competition::where('status', 'public')
            ->orderBy('start_date', 'desc')
            ->skip($skip)
            ->take($take)
            ->get();

        $total = Competition::where('status', 'public')->count();

        return response()->json([
            'data' => $competitions,
            'meta' => [
                'skip' => $skip,
                'take' => $take,
                'total' => $total,
                'hasMore' => ($skip + $take) < $total
            ]
        ]);
    }

    /**
     * Get competitions/tournaments for the authenticated user with pagination.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function getUserCompetitions(Request $request): JsonResponse
    {
        $request->validate([
            'skip' => 'integer|min:0',
            'take' => 'integer|min:1|max:100',
        ]);

        $skip = $request->input('skip', 0);
        $take = $request->input('take', 10);
        $user = $request->user();

        if (!$user) {
            return response()->json(['error' => 'Unauthenticated'], 401);
        }

        $query = Competition::whereHas('teams', function ($query) use ($user) {
            $query->whereHas('users', function ($query) use ($user) {
                $query->where('users.id', $user->id);
            });
        });

        $competitions = $query->orderBy('start_date', 'desc')
            ->skip($skip)
            ->take($take)
            ->get();

        $total = $query->count();

        return response()->json([
            'data' => $competitions,
            'meta' => [
                'skip' => $skip,
                'take' => $take,
                'total' => $total,
                'hasMore' => ($skip + $take) < $total
            ]
        ]);
    }
}
