import {
  Body,
  Controller,
  Get,
  Param,
  Patch,
  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 type {
  CreateOrderDto,
  LocalOrderAction,
  LocalOrderActionDto,
  OrderListQuery,
  ThirdPartyOrderAction,
  ThirdPartyOrderActionDto,
  ThirdPartyOrderListSyncDto,
  ThirdPartyCourseQueryDto,
} from './dto/orders.dto';
import { OrdersService } from './orders.service';

@UseGuards(AuthGuard)
@Controller('api/admin/orders')
export class OrdersController {
  constructor(private readonly ordersService: OrdersService) {}

  @Post('course-query')
  @OperationLog({
    action: 'orders.course_query',
    resourceType: 'order',
    metadataFromBody: ['productId', 'school', 'account'],
    description: '第三方查课',
  })
  queryThirdPartyCourses(
    @Req() request: AuthenticatedRequest,
    @Body() dto: ThirdPartyCourseQueryDto,
  ) {
    return this.ordersService.queryThirdPartyCourses(request.user, dto);
  }

  @Post()
  @OperationLog({
    action: 'orders.create',
    resourceType: 'order',
    resourceIdFromResult: 'id',
    metadataFromBody: ['productId', 'quantity', 'remark'],
    description: '创建订单',
  })
  createOrder(
    @Req() request: AuthenticatedRequest,
    @Body() dto: CreateOrderDto,
  ) {
    return this.ordersService.createOrder(request.user, dto);
  }

  @Get()
  listOrders(
    @Req() request: AuthenticatedRequest,
    @Query() query: OrderListQuery,
  ) {
    return this.ordersService.listOrders(request.user, query);
  }

  @Get(':idOrNo')
  getOrderDetail(
    @Req() request: AuthenticatedRequest,
    @Param('idOrNo') idOrNo: string,
  ) {
    return this.ordersService.getOrderDetail(request.user, idOrNo);
  }

  @Patch(':idOrNo/mark-paid')
  @OperationLog({
    action: 'orders.mark_paid',
    resourceType: 'order',
    resourceIdParam: 'idOrNo',
    description: '标记订单已支付并触发履约',
  })
  markOrderPaid(
    @Req() request: AuthenticatedRequest,
    @Param('idOrNo') idOrNo: string,
  ) {
    return this.ordersService.markOrderPaid(request.user, idOrNo);
  }

  @Post('third-party/sync-list')
  @OperationLog({
    action: 'orders.third_party.sync_list',
    resourceType: 'order',
    metadataFromBody: ['page', 'limit', 'recent'],
    description: '拉取第三方订单列表并同步本地订单',
  })
  syncThirdPartyOrderList(
    @Req() request: AuthenticatedRequest,
    @Body() dto: ThirdPartyOrderListSyncDto,
  ) {
    return this.ordersService.syncThirdPartyOrderList(request.user, dto);
  }

  @Post(':idOrNo/third-party/refresh')
  @OperationLog({
    action: 'orders.third_party.refresh',
    resourceType: 'order',
    resourceIdParam: 'idOrNo',
    metadataFromBody: ['externalOrderNo', 'username'],
    description: '第三方订单查单刷新',
  })
  refreshThirdPartyOrder(
    @Req() request: AuthenticatedRequest,
    @Param('idOrNo') idOrNo: string,
    @Body() dto: ThirdPartyOrderActionDto,
  ) {
    return this.ordersService.refreshThirdPartyOrder(
      request.user,
      idOrNo,
      dto,
    );
  }

  @Post(':idOrNo/third-party/:action')
  @OperationLog({
    action: 'orders.third_party.action',
    resourceType: 'order',
    resourceIdParam: 'idOrNo',
    metadataFromBody: [
      'externalOrderNo',
      'username',
      'convertToClassId',
      'time',
      'cycle',
      'autoReset',
    ],
    description: '第三方订单操作',
  })
  runThirdPartyOrderAction(
    @Req() request: AuthenticatedRequest,
    @Param('idOrNo') idOrNo: string,
    @Param('action') action: ThirdPartyOrderAction,
    @Body() dto: ThirdPartyOrderActionDto,
  ) {
    return this.ordersService.runThirdPartyOrderAction(
      request.user,
      idOrNo,
      action,
      dto,
    );
  }

  @Patch(':idOrNo/local/:action')
  @OperationLog({
    action: 'orders.local.action',
    resourceType: 'order',
    resourceIdParam: 'idOrNo',
    metadataFromBody: ['remark'],
    description: '自营订单本地操作',
  })
  runLocalOrderAction(
    @Req() request: AuthenticatedRequest,
    @Param('idOrNo') idOrNo: string,
    @Param('action') action: LocalOrderAction,
    @Body() dto: LocalOrderActionDto,
  ) {
    return this.ordersService.runLocalOrderAction(
      request.user,
      idOrNo,
      action,
      dto,
    );
  }
}
