import { Controller, Get, Post, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, RolesGuard, Roles, UserRole } from '@genex/common';
import { AdminFinanceService } from '../../../application/services/admin-finance.service';
import { ListSettlementsQueryDto } from '../dto/settlement.dto';
import { ListRefundsQueryDto } from '../dto/refund.dto';

@ApiTags('Admin - Finance')
@Controller('admin/finance')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminFinanceController {
  constructor(private readonly adminFinanceService: AdminFinanceService) {}

  @Get('summary')
  @ApiOperation({ summary: 'Platform finance overview (fees, settlements, refunds, pool balance)' })
  async getSummary() {
    return { code: 0, data: await this.adminFinanceService.getSummary() };
  }

  @Get('settlements')
  @ApiOperation({ summary: 'Settlement queue (paginated, filter by status)' })
  async getSettlements(@Query() query: ListSettlementsQueryDto) {
    return {
      code: 0,
      data: await this.adminFinanceService.getSettlements(
        +(query.page || '1'),
        +(query.limit || '20'),
        query.status,
      ),
    };
  }

  @Get('revenue-trend')
  @ApiOperation({ summary: 'Monthly revenue trend data (last 12 months)' })
  async getRevenueTrend() {
    return { code: 0, data: await this.adminFinanceService.getRevenueTrend() };
  }

  @Post('settlements/:id/process')
  @ApiOperation({ summary: 'Process a pending settlement' })
  async processSettlement(@Param('id') id: string) {
    return { code: 0, data: await this.adminFinanceService.processSettlement(id) };
  }

  @Post('settlements/:id/cancel')
  @ApiOperation({ summary: 'Cancel a pending settlement' })
  async cancelSettlement(@Param('id') id: string) {
    return { code: 0, data: await this.adminFinanceService.cancelSettlement(id) };
  }

  @Get('consumer-refunds')
  @ApiOperation({ summary: 'Consumer refund tracking (paginated)' })
  async getConsumerRefunds(@Query() query: ListRefundsQueryDto) {
    return {
      code: 0,
      data: await this.adminFinanceService.getConsumerRefunds(
        +(query.page || '1'),
        +(query.limit || '20'),
        query.status,
      ),
    };
  }
}
