<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\BadgeResource;
use App\Models\Badge;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use OpenApi\Attributes as OA;

class BadgeController extends Controller
{
    #[OA\Get(
        path: "/api/badges",
        tags: ["Gamification"],
        summary: "List all badges",
        description: "Lists badges and indicates if the current user has unlocked them (if logged in)",
        security: [["bearerAuth" => []]]
    )]
    #[OA\Response(
        response: 200,
        description: "List of badges",
        content: new OA\JsonContent(
            properties: [
                new OA\Property(property: "data", type: "array", items: new OA\Items(type: "object"))
            ]
        )
    )]
    public function index()
    {
        $badges = Badge::where('status', 'active')->orderBy('sort_order')->get();

        // Eager load user badges if logged in to avoid N+1 in Resource
        if (Auth::check()) {
            Auth::user()->load('userBadges');
        }

        return BadgeResource::collection($badges);
    }
}
