import { Controller, Get, Post, Patch, Body, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiQuery, ApiParam } from '@nestjs/swagger';
import { AdminService } from './admin.service';
import { SuperAdminGuard } from '../../common/guards/super-admin.guard';

@ApiTags('admin')
@ApiBearerAuth()
@UseGuards(SuperAdminGuard)
@Controller('admin')
export class AdminController {
  constructor(private readonly adminService: AdminService) {}

  @Get('stats')
  @ApiOperation({ summary: 'Get platform statistics' })
  async getStats() {
    return this.adminService.getStats();
  }

  @Get('licenses')
  @ApiOperation({ summary: 'Get paginated list of licenses' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  async getLicenses(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
    @Query('search') search?: string,
  ) {
    const p = page ? parseInt(page, 10) : 1;
    const l = limit ? parseInt(limit, 10) : 25;
    return this.adminService.getLicenses(p, l, search);
  }

  @Get('licenses/:id')
  @ApiOperation({ summary: 'Get license details by ID' })
  @ApiParam({ name: 'id', description: 'License ID' })
  async getLicenseById(@Param('id') id: string) {
    return this.adminService.getLicenseById(id);
  }

  @Post('licenses')
  @ApiOperation({ summary: 'Create a new license' })
  async createLicense(@Body('email') email: string) {
    return this.adminService.createLicense(email);
  }

  @Get('users')
  @ApiOperation({ summary: 'Get paginated list of users' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getUsers(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    const p = page ? parseInt(page, 10) : 1;
    const l = limit ? parseInt(limit, 10) : 25;
    return this.adminService.getUsers(p, l);
  }

  @Patch('users/:id')
  @ApiOperation({ summary: 'Update user (admin only)' })
  @ApiParam({ name: 'id', description: 'User ID' })
  async updateUser(
    @Param('id') userId: string,
    @Body() data: { is_super_admin?: boolean; email_verified?: boolean },
  ) {
    return this.adminService.updateUser(userId, data);
  }

  @Get('subscriptions')
  @ApiOperation({ summary: 'Get all webstore subscriptions' })
  async getSubscriptions() {
    return this.adminService.getSubscriptions();
  }

  @Get('servers')
  @ApiOperation({ summary: 'Get all server connections' })
  async getServers() {
    return this.adminService.getServers();
  }
}
