import {
  Controller,
  Get,
  Post,
  Patch,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiResponse,
  ApiParam,
} from '@nestjs/swagger';
import { LicensesService } from './licenses.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RolesGuard } from '../auth/guards/roles.guard';
import { Roles, UserRole } from '../../common/decorators/roles.decorator';
import {
  CurrentUser,
  JwtPayload,
} from '../../common/decorators/current-user.decorator';
import {
  CreateLicenseDto,
  UpdateLicenseDto,
  RenewLicenseDto,
  FindLicensesDto,
} from './dto';

@ApiTags('Licenses')
@ApiBearerAuth()
@Controller('licenses')
@UseGuards(JwtAuthGuard)
export class LicensesController {
  constructor(private readonly licensesService: LicensesService) {}

  @Get()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'List all licenses with pagination' })
  @ApiResponse({ status: 200, description: 'Paginated list of licenses' })
  async findAll(@Query() query: FindLicensesDto) {
    return this.licensesService.findAll(query);
  }

  @Get('my')
  @UseGuards(RolesGuard)
  @Roles(UserRole.CLUB_ADMIN)
  @ApiOperation({ summary: 'Get license for current club' })
  @ApiResponse({ status: 200, description: 'License details with status' })
  @ApiResponse({ status: 404, description: 'License not found' })
  async findMyLicense(@CurrentUser() user: JwtPayload) {
    return this.licensesService.findByClubId(user.clubId);
  }

  @Get('club/:clubId')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Get license by club ID' })
  @ApiParam({ name: 'clubId', description: 'Club UUID' })
  @ApiResponse({ status: 200, description: 'License details' })
  @ApiResponse({ status: 404, description: 'License not found' })
  async findByClubId(@Param('clubId', ParseUUIDPipe) clubId: string) {
    return this.licensesService.findByClubId(clubId);
  }

  @Get(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Get license by ID' })
  @ApiParam({ name: 'id', description: 'License UUID' })
  @ApiResponse({ status: 200, description: 'License details' })
  @ApiResponse({ status: 404, description: 'License not found' })
  async findById(@Param('id', ParseUUIDPipe) id: string) {
    return this.licensesService.findById(id);
  }

  @Post()
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Create a new license for a club' })
  @ApiResponse({ status: 201, description: 'License created' })
  @ApiResponse({ status: 400, description: 'Club already has a license' })
  @ApiResponse({ status: 404, description: 'Club not found' })
  async create(@Body() dto: CreateLicenseDto) {
    return this.licensesService.create(dto);
  }

  @Patch(':id')
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Update license details' })
  @ApiParam({ name: 'id', description: 'License UUID' })
  @ApiResponse({ status: 200, description: 'License updated' })
  @ApiResponse({ status: 404, description: 'License not found' })
  async update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateLicenseDto,
  ) {
    return this.licensesService.update(id, dto);
  }

  @Post(':id/renew')
  @HttpCode(HttpStatus.OK)
  @UseGuards(RolesGuard)
  @Roles(UserRole.SUPER_ADMIN)
  @ApiOperation({ summary: 'Renew a license' })
  @ApiParam({ name: 'id', description: 'License UUID' })
  @ApiResponse({ status: 200, description: 'License renewed' })
  @ApiResponse({ status: 400, description: 'Invalid renewal date' })
  @ApiResponse({ status: 404, description: 'License not found' })
  async renew(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: RenewLicenseDto,
  ) {
    return this.licensesService.renew(id, dto);
  }
}
