import {
  Controller, Get, Post, Put, Delete, Param, Body, UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, Roles, RolesGuard, UserRole } from '@genex/common';
import { AdminSystemService } from '../../../application/services/admin-system.service';
import { CreateAdminDto, UpdateAdminRoleDto } from '../dto/admin-system.dto';

@ApiTags('Admin - System Management')
@Controller('admin/system')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminSystemController {
  constructor(private readonly systemService: AdminSystemService) {}

  @Get('admins')
  @ApiOperation({ summary: 'List all admin accounts' })
  async listAdmins() {
    const data = await this.systemService.listAdmins();
    return { code: 0, data };
  }

  @Post('admins')
  @ApiOperation({ summary: 'Create a new admin account' })
  async createAdmin(@Body() dto: CreateAdminDto) {
    const data = await this.systemService.createAdmin(dto);
    return { code: 0, data };
  }

  @Put('admins/:id/role')
  @ApiOperation({ summary: 'Update admin sub-role' })
  async updateAdminRole(
    @Param('id') id: string,
    @Body() dto: UpdateAdminRoleDto,
  ) {
    const data = await this.systemService.updateAdminRole(id, dto.role);
    return { code: 0, data };
  }

  @Delete('admins/:id')
  @ApiOperation({ summary: 'Deactivate an admin account' })
  async deactivateAdmin(@Param('id') id: string) {
    const data = await this.systemService.deactivateAdmin(id);
    return { code: 0, data };
  }

  @Get('config')
  @ApiOperation({ summary: 'Get platform configuration' })
  async getConfig() {
    const data = await this.systemService.getConfig();
    return { code: 0, data };
  }

  @Put('config')
  @ApiOperation({ summary: 'Update platform configuration' })
  async updateConfig(@Body() body: any) {
    const data = await this.systemService.updateConfig(body);
    return { code: 0, data };
  }

  @Get('health')
  @ApiOperation({ summary: 'Get health status of all microservices' })
  async getServiceHealth() {
    const data = await this.systemService.getServiceHealth();
    return { code: 0, data };
  }
}
