import { Body, Controller, Delete, Get, Param, Patch, Post, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { PaginationQueryDto } from '../../common/dto/pagination-query.dto';
import { SuperAdminPermissions } from '../../common/decorators/superadmin-permissions.decorator';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { SuperAdminPermissionsGuard } from '../../common/guards/superadmin-permissions.guard';
import { SuperAdminJwtAuthGuard } from '../../common/guards/super-admin-jwt-auth.guard';
import { JwtPayload } from '../../common/interfaces/jwt-payload.interface';
import { BulkSuperAdminActionDto } from './dto/bulk-superadmin-action.dto';
import { CreateSuperAdminCaseDto } from './dto/create-superadmin-case.dto';
import { SuperAdminCaseQueryDto } from './dto/superadmin-case-query.dto';
import { SuperAdminChartsQueryDto } from './dto/superadmin-charts-query.dto';
import { SuperAdminRecentActivityQueryDto } from './dto/superadmin-recent-activity-query.dto';
import { SuperAdminReportsQueryDto } from './dto/superadmin-reports-query.dto';
import { UpdateContentModerationStatusDto } from './dto/update-content-moderation-status.dto';
import { UpdateSuperAdminCaseDto } from './dto/update-superadmin-case.dto';
import { UpdateSuperAdminSettingsDto } from './dto/update-superadmin-settings.dto';
import { UpdateSuperAdminUserStatusDto } from './dto/update-superadmin-user-status.dto';
import { SUPERADMIN_PERMISSIONS } from './superadmin-permissions';
import { SuperAdminService } from './superadmin.service';

@ApiTags('SuperAdmin')
@ApiBearerAuth()
@UseGuards(SuperAdminJwtAuthGuard, SuperAdminPermissionsGuard)
@Controller('superadmin')
export class SuperAdminController {
  constructor(private readonly superAdminService: SuperAdminService) {}

  @Get('session')
  getSession(@CurrentUser() user: JwtPayload) {
    return this.superAdminService.getSession(user);
  }

  @Get('overview')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.OVERVIEW_READ)
  async getOverview() {
    return this.superAdminService.getOverview();
  }

  @Get('charts')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.ANALYTICS_READ)
  async getCharts(@Query() query: SuperAdminChartsQueryDto) {
    return this.superAdminService.getCharts(query);
  }

  @Get('recent-activity')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.ANALYTICS_READ)
  async getRecentActivity(@Query() query: SuperAdminRecentActivityQueryDto) {
    return this.superAdminService.getRecentActivity(query);
  }

  @Get('reports')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.ANALYTICS_READ)
  async getReports(@Query() query: SuperAdminReportsQueryDto) {
    return this.superAdminService.getReports(query);
  }

  @Get('ops')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.OPS_READ)
  async getOps() {
    return this.superAdminService.getOps();
  }

  @Get('cases')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CASES_MANAGE)
  async getCases(@Query() query: SuperAdminCaseQueryDto) {
    return this.superAdminService.getCases(query);
  }

  @Post('cases')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CASES_MANAGE)
  async createCase(@CurrentUser() user: JwtPayload, @Body() dto: CreateSuperAdminCaseDto) {
    return this.superAdminService.createCase(user.email ?? user.sub, dto);
  }

  @Patch('cases/:caseId')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CASES_MANAGE)
  async updateCase(
    @CurrentUser() user: JwtPayload,
    @Param('caseId') caseId: string,
    @Body() dto: UpdateSuperAdminCaseDto,
  ) {
    return this.superAdminService.updateCase(user.email ?? user.sub, caseId, dto);
  }

  @Post('bulk-actions')
  @SuperAdminPermissions(
    SUPERADMIN_PERMISSIONS.CASES_MANAGE,
    SUPERADMIN_PERMISSIONS.CONTENT_MODERATE,
  )
  async performBulkAction(
    @CurrentUser() user: JwtPayload,
    @Body() dto: BulkSuperAdminActionDto,
  ) {
    return this.superAdminService.performBulkAction(user.email ?? user.sub, dto);
  }

  @Get('settings')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.SETTINGS_READ)
  async getSettings(): Promise<Record<string, unknown>> {
    return this.superAdminService.getSettings();
  }

  @Get('settings/history')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.SETTINGS_READ)
  async getSettingsHistory(@Query() query: PaginationQueryDto) {
    return this.superAdminService.getSettingsHistory(query);
  }

  @Patch('settings')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.SETTINGS_WRITE)
  async updateSettings(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdateSuperAdminSettingsDto,
  ): Promise<Record<string, unknown>> {
    return this.superAdminService.updateSettings(user.email ?? user.sub, dto);
  }

  @Post('settings/history/:historyId/restore')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.SETTINGS_WRITE)
  async restoreSettingsVersion(
    @CurrentUser() user: JwtPayload,
    @Param('historyId') historyId: string,
  ) {
    return this.superAdminService.restoreSettingsVersion(user.email ?? user.sub, historyId);
  }

  @Patch('posts/:postId/status')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async updatePostStatus(
    @CurrentUser() user: JwtPayload,
    @Param('postId') postId: string,
    @Body() dto: UpdateContentModerationStatusDto,
  ) {
    return this.superAdminService.updatePostStatus(user.email ?? user.sub, postId, dto);
  }

  @Delete('posts/:postId')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async deletePost(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.superAdminService.deletePost(user.email ?? user.sub, postId);
  }

  @Post('posts/:postId/restore')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async restorePost(@CurrentUser() user: JwtPayload, @Param('postId') postId: string) {
    return this.superAdminService.restorePost(user.email ?? user.sub, postId);
  }

  @Patch('comments/:commentId/status')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async updateCommentStatus(
    @CurrentUser() user: JwtPayload,
    @Param('commentId') commentId: string,
    @Body() dto: UpdateContentModerationStatusDto,
  ) {
    return this.superAdminService.updateCommentStatus(user.email ?? user.sub, commentId, dto);
  }

  @Delete('comments/:commentId')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async deleteComment(@CurrentUser() user: JwtPayload, @Param('commentId') commentId: string) {
    return this.superAdminService.deleteComment(user.email ?? user.sub, commentId);
  }

  @Post('comments/:commentId/restore')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.CONTENT_MODERATE)
  async restoreComment(
    @CurrentUser() user: JwtPayload,
    @Param('commentId') commentId: string,
  ) {
    return this.superAdminService.restoreComment(user.email ?? user.sub, commentId);
  }

  @Patch('users/:userId/status')
  @SuperAdminPermissions(SUPERADMIN_PERMISSIONS.USERS_MANAGE)
  async updateUserStatus(
    @CurrentUser() user: JwtPayload,
    @Param('userId') userId: string,
    @Body() dto: UpdateSuperAdminUserStatusDto,
  ) {
    return this.superAdminService.updateUserStatus(user.email ?? user.sub, userId, dto);
  }
}
