import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
} from "@nestjs/common";
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
} from "@nestjs/swagger";
import { Roles } from "../../common/decorators";
import { LeagueTiersService } from "./league-tiers.service";

@ApiTags("League Tiers")
@ApiBearerAuth()
@Controller("admin/league-tiers")
@Roles("superadmin")
export class LeagueTiersController {
  constructor(private readonly leagueTiersService: LeagueTiersService) {}

  // ─── READ ──────────────────────────────────────────────────

  @Get()
  @ApiOperation({ summary: "Get all league tiers" })
  @ApiResponse({ status: 200, description: "All league tiers" })
  @ApiQuery({
    name: "active",
    required: false,
    type: Boolean,
    description: "Filter active only",
  })
  async findAll(@Query("active") active?: string) {
    if (active === "true") {
      return this.leagueTiersService.findActive();
    }
    return this.leagueTiersService.findAll();
  }

  @Get("stats")
  @ApiOperation({ summary: "Get league tier statistics" })
  @ApiResponse({ status: 200, description: "Tier statistics" })
  async getStats() {
    return this.leagueTiersService.getStats();
  }

  @Get("tier/:tier")
  @ApiOperation({ summary: "Get leagues by tier level" })
  @ApiParam({ name: "tier", description: "Tier level (1=Diamond, 2=Gold, 3=Silver)" })
  async findByTier(@Param("tier") tier: string) {
    return this.leagueTiersService.findByTier(parseInt(tier, 10));
  }

  // ─── WRITE ─────────────────────────────────────────────────

  @Post()
  @ApiOperation({ summary: "Add a league to tiers (upsert)" })
  @ApiResponse({ status: 201, description: "League added/updated" })
  async addLeague(
    @Body()
    body: {
      leagueId: string;
      tier?: number;
      notes?: string;
      addedBy?: string;
    },
  ) {
    return this.leagueTiersService.addLeague(
      body.leagueId,
      body.tier,
      body.notes,
      body.addedBy,
    );
  }

  @Put(":leagueId/tier")
  @ApiOperation({ summary: "Update league tier level" })
  @ApiParam({ name: "leagueId", description: "League ID" })
  async updateTier(
    @Param("leagueId") leagueId: string,
    @Body() body: { tier: number },
  ) {
    return this.leagueTiersService.updateTier(leagueId, body.tier);
  }

  @Put(":leagueId/deactivate")
  @ApiOperation({ summary: "Deactivate a league (soft remove)" })
  @ApiParam({ name: "leagueId", description: "League ID" })
  async removeLeague(@Param("leagueId") leagueId: string) {
    return this.leagueTiersService.removeLeague(leagueId);
  }

  @Delete(":leagueId")
  @ApiOperation({ summary: "Permanently delete a league tier" })
  @ApiParam({ name: "leagueId", description: "League ID" })
  async deleteLeague(@Param("leagueId") leagueId: string) {
    return this.leagueTiersService.deleteLeague(leagueId);
  }

  // ─── SYNC & RETRAIN ───────────────────────────────────────

  @Post("sync")
  @ApiOperation({ summary: "Sync qualified_leagues.json from DB" })
  @ApiResponse({ status: 200, description: "Sync result with count and path" })
  async syncQualifiedLeagues() {
    return this.leagueTiersService.syncQualifiedLeagues();
  }

  @Post("retrain")
  @ApiOperation({ summary: "Trigger AI model retrain" })
  @ApiResponse({ status: 200, description: "Retrain trigger result" })
  async triggerRetrain() {
    return this.leagueTiersService.triggerModelRetrain();
  }
}
