<?php

namespace App\Http\Controllers;

use App\Services\LeaderboardService;
use App\Services\BadgeService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class LeaderboardController extends Controller
{
    protected LeaderboardService $leaderboardService;
    protected BadgeService $badgeService;

    public function __construct(LeaderboardService $leaderboardService, BadgeService $badgeService)
    {
        $this->leaderboardService = $leaderboardService;
        $this->badgeService = $badgeService;
    }

    public function index(Request $request): JsonResponse
    {
        $type = $request->get('type', 'top_buyer');
        $period = $request->get('period', 'monthly');
        $limit = $request->get('limit', 10);
        $country = $request->get('country');

        $leaderboard = $this->leaderboardService->getLeaderboard($type, $period, $limit, $country);

        return response()->json([
            'type' => $type,
            'period' => $period,
            'leaderboard' => $leaderboard,
        ]);
    }

    public function types(): JsonResponse
    {
        return response()->json([
            'types' => [
                ['key' => 'top_buyer', 'name' => 'Meilleur Acheteur'],
                ['key' => 'top_seller', 'name' => 'Meilleur Vendeur'],
                ['key' => 'top_sponsor', 'name' => 'Meilleur Parrain'],
                ['key' => 'top_leader', 'name' => 'Meilleur Leader MLM'],
            ],
            'periods' => [
                ['key' => 'weekly', 'name' => 'Cette semaine'],
                ['key' => 'monthly', 'name' => 'Ce mois'],
                ['key' => 'quarterly', 'name' => 'Ce trimestre'],
                ['key' => 'yearly', 'name' => 'Cette année'],
            ],
        ]);
    }

    public function myRank(Request $request): JsonResponse
    {
        $user = $request->user();
        $type = $request->get('type', 'top_buyer');
        $period = $request->get('period', 'monthly');

        $rank = $this->leaderboardService->getUserRank($user->id, $type, $period);

        return response()->json([
            'rank' => $rank,
        ]);
    }

    public function calculate(Request $request): JsonResponse
    {
        $period = $request->get('period', 'monthly');
        
        $this->leaderboardService->calculateLeaderboards($period);
        $this->badgeService->awardAutomaticBadges();

        return response()->json([
            'success' => true,
            'message' => 'Classements calculés et badges attribués',
        ]);
    }

    public function badges(Request $request): JsonResponse
    {
        $user = $request->user();
        $badges = $this->badgeService->getUserBadges($user->id);

        return response()->json([
            'badges' => $badges,
        ]);
    }

    public function recentBadges(): JsonResponse
    {
        $badges = $this->badgeService->getRecentBadges(10);

        return response()->json([
            'badges' => $badges,
        ]);
    }
}
