import {
  Controller,
  Get,
  Param,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, RolesGuard } from '@/common';
import { Roles } from '@/common/decorators/roles.decorator';
import { TransactionService } from '@/modules/shared/finance/transaction.service';
import {
  QueryUserTransactionDto,
  QueryMerchantTransactionDto,
  QueryPlatformTransactionDto,
} from '@/modules/shared/finance/dto/transaction.dto';

@ApiTags('交易流水管理(管理员)')
@Controller('admin/finance/transactions')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@ApiBearerAuth()
export class TransactionAdminController {
  constructor(private readonly transactionService: TransactionService) {}

  // ==================== 平台交易流水 ====================

  @Get('platform')
  @ApiOperation({ summary: '查询平台交易流水' })
  async getPlatformTransactions(@Query() dto: QueryPlatformTransactionDto) {
    return this.transactionService.getPlatformTransactions({
      accountId: dto.accountId,
      direction: dto.direction,
      transactionType: dto.transactionType,
      businessType: dto.businessType,
      startDate: dto.startDate,
      endDate: dto.endDate,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  @Get('platform/:id')
  @ApiOperation({ summary: '查询平台交易详情' })
  async getPlatformTransactionDetail(@Param('id') id: number) {
    // TODO: 实现根据ID查询交易详情
    return { message: '功能开发中' };
  }

  @Get('platform/export')
  @ApiOperation({ summary: '导出平台交易流水' })
  async exportPlatformTransactions(@Query() dto: QueryPlatformTransactionDto) {
    // TODO: 实现导出功能
    return { message: '导出功能开发中' };
  }

  // ==================== 用户交易流水 ====================

  @Get('users')
  @ApiOperation({ summary: '查询用户交易流水' })
  async getUserTransactions(@Query() dto: QueryUserTransactionDto) {
    return this.transactionService.getUserTransactions({
      userId: dto.userId,
      direction: dto.direction,
      transactionType: dto.transactionType,
      businessType: dto.businessType,
      startDate: dto.startDate,
      endDate: dto.endDate,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  @Get('users/:userId')
  @ApiOperation({ summary: '查询指定用户交易流水' })
  async getUserTransactionsByUserId(
    @Param('userId') userId: number,
    @Query() dto: QueryUserTransactionDto,
  ) {
    return this.transactionService.getUserTransactions({
      userId,
      direction: dto.direction,
      transactionType: dto.transactionType,
      businessType: dto.businessType,
      startDate: dto.startDate,
      endDate: dto.endDate,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  // ==================== 商家交易流水 ====================

  @Get('merchants')
  @ApiOperation({ summary: '查询商家交易流水' })
  async getMerchantTransactions(@Query() dto: QueryMerchantTransactionDto) {
    return this.transactionService.getMerchantTransactions({
      merchantId: dto.merchantId,
      direction: dto.direction,
      transactionType: dto.transactionType,
      businessType: dto.businessType,
      startDate: dto.startDate,
      endDate: dto.endDate,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  @Get('merchants/:merchantId')
  @ApiOperation({ summary: '查询指定商家交易流水' })
  async getMerchantTransactionsByMerchantId(
    @Param('merchantId') merchantId: number,
    @Query() dto: QueryMerchantTransactionDto,
  ) {
    return this.transactionService.getMerchantTransactions({
      merchantId,
      direction: dto.direction,
      transactionType: dto.transactionType,
      businessType: dto.businessType,
      startDate: dto.startDate,
      endDate: dto.endDate,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }
}
