import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiQuery, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard.js';
import { EquipmentService } from './equipment.service.js';

@ApiTags('Equipment')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('equipment')
export class EquipmentController {
  constructor(private readonly equipmentService: EquipmentService) {}

  @Get()
  @ApiOperation({ summary: 'Search and browse equipment' })
  @ApiQuery({ name: 'query', required: false, description: 'Search term for name' })
  @ApiQuery({ name: 'category', required: false, description: 'Filter by category (Weapons, Armor, Consumables, etc.)' })
  @ApiQuery({ name: 'subcategory', required: false, description: 'Filter by subcategory' })
  @ApiQuery({ name: 'minLevel', required: false, type: Number })
  @ApiQuery({ name: 'maxLevel', required: false, type: Number })
  @ApiQuery({ name: 'traits', required: false, description: 'Comma-separated list of traits' })
  @ApiQuery({ name: 'page', required: false, type: Number, description: 'Page number (default: 1)' })
  @ApiQuery({ name: 'limit', required: false, type: Number, description: 'Items per page (default: 50)' })
  async search(
    @Query('query') query?: string,
    @Query('category') category?: string,
    @Query('subcategory') subcategory?: string,
    @Query('minLevel') minLevel?: string,
    @Query('maxLevel') maxLevel?: string,
    @Query('traits') traits?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.equipmentService.search({
      query,
      category,
      subcategory,
      minLevel: minLevel ? parseInt(minLevel, 10) : undefined,
      maxLevel: maxLevel ? parseInt(maxLevel, 10) : undefined,
      traits: traits ? traits.split(',').map(t => t.trim()) : undefined,
      page: page ? parseInt(page, 10) : 1,
      limit: limit ? parseInt(limit, 10) : 50,
    });
  }

  @Get('categories')
  @ApiOperation({ summary: 'Get all equipment categories' })
  async getCategories() {
    return this.equipmentService.getCategories();
  }

  @Get('categories/:category/subcategories')
  @ApiOperation({ summary: 'Get subcategories for a category' })
  async getSubcategories(@Param('category') category: string) {
    return this.equipmentService.getSubcategories(category);
  }

  @Get('weapons')
  @ApiOperation({ summary: 'Browse weapons' })
  @ApiQuery({ name: 'query', required: false })
  @ApiQuery({ name: 'subcategory', required: false })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getWeapons(
    @Query('query') query?: string,
    @Query('subcategory') subcategory?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.equipmentService.getWeapons({
      query,
      subcategory,
      page: page ? parseInt(page, 10) : 1,
      limit: limit ? parseInt(limit, 10) : 50,
    });
  }

  @Get('armor')
  @ApiOperation({ summary: 'Browse armor' })
  @ApiQuery({ name: 'query', required: false })
  @ApiQuery({ name: 'subcategory', required: false })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getArmor(
    @Query('query') query?: string,
    @Query('subcategory') subcategory?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.equipmentService.getArmor({
      query,
      subcategory,
      page: page ? parseInt(page, 10) : 1,
      limit: limit ? parseInt(limit, 10) : 50,
    });
  }

  @Get('consumables')
  @ApiOperation({ summary: 'Browse consumables' })
  @ApiQuery({ name: 'query', required: false })
  @ApiQuery({ name: 'subcategory', required: false })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getConsumables(
    @Query('query') query?: string,
    @Query('subcategory') subcategory?: string,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.equipmentService.getConsumables({
      query,
      subcategory,
      page: page ? parseInt(page, 10) : 1,
      limit: limit ? parseInt(limit, 10) : 50,
    });
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get equipment by ID' })
  async getById(@Param('id') id: string) {
    return this.equipmentService.getById(id);
  }

  @Get('by-name/:name')
  @ApiOperation({ summary: 'Get equipment by exact name' })
  async getByName(@Param('name') name: string) {
    return this.equipmentService.getByName(decodeURIComponent(name));
  }
}
