import { Controller, Get, Post, Query, Req, UseGuards } from '@nestjs/common';
import { OperationLog } from '../audit/operation-log.decorator';
import { AuthGuard } from '../auth/auth.guard';
import type { AuthenticatedRequest } from '../auth/types/authenticated-request';
import { ThirdPartyAdminService } from './third-party-admin.service';

@UseGuards(AuthGuard)
@Controller('api/admin/third-party')
export class ThirdPartyAdminController {
  constructor(private readonly thirdPartyAdminService: ThirdPartyAdminService) {}

  @Get('config-status')
  getConfigStatus(@Req() request: AuthenticatedRequest) {
    return this.thirdPartyAdminService.getConfigStatus(request.user);
  }

  @Post('test-connection')
  @OperationLog({
    action: 'third_party.test_connection',
    resourceType: 'third_party',
    description: '测试第三方接口连接',
  })
  testConnection(@Req() request: AuthenticatedRequest) {
    return this.thirdPartyAdminService.testConnection(request.user);
  }

  @Get('api-call-logs')
  listApiCallLogs(
    @Req() request: AuthenticatedRequest,
    @Query() query: Record<string, unknown>,
  ) {
    return this.thirdPartyAdminService.listApiCallLogs(request.user, query);
  }

  @Post('categories/sync')
  @OperationLog({
    action: 'third_party.categories.sync',
    resourceType: 'category',
    description: '同步第三方分类',
  })
  syncCategories(@Req() request: AuthenticatedRequest) {
    return this.thirdPartyAdminService.syncCategories(request.user);
  }

  @Get('categories')
  listCategories(
    @Req() request: AuthenticatedRequest,
    @Query() query: Record<string, unknown>,
  ) {
    return this.thirdPartyAdminService.listCategories(request.user, query);
  }

  @Post('courses/sync')
  @OperationLog({
    action: 'third_party.courses.sync',
    resourceType: 'course',
    metadataFromQuery: ['remoteCategoryId'],
    description: '同步第三方课程',
  })
  syncCourses(
    @Req() request: AuthenticatedRequest,
    @Query() query: Record<string, unknown>,
  ) {
    return this.thirdPartyAdminService.syncCourses(request.user, query);
  }

  @Get('courses')
  listCourses(
    @Req() request: AuthenticatedRequest,
    @Query() query: Record<string, unknown>,
  ) {
    return this.thirdPartyAdminService.listCourses(request.user, query);
  }
}
