import { Controller, Post, Get, Put, Delete, Param, Body, UseGuards } from '@nestjs/common';
import { OperationAgentService } from '../../core/operation/OperationAgentService';
import { StoreBindingDto } from '../dto/StoreBindingDto';
import { Store } from '../../entities/Store';
import { RbacGuard } from '../../core/guards/rbac.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('Operation-Agent')
@ApiBearerAuth()
@Controller('api/operation-agent')
export class OperationAgentController {
  constructor(private operationAgentService: OperationAgentService) {}

  /**
   * 绑定店铺
   */
  @Post('stores')
  @UseGuards(RbacGuard)
  async bindStore(@Body() dto: StoreBindingDto): Promise<Store> {
    return this.operationAgentService.bindStore(dto);
  }

  /**
   * 获取商户的店铺列表
   */
  @Get('stores/:merchantId')
  @UseGuards(RbacGuard)
  async getStores(@Param('merchantId') merchantId: string): Promise<Store[]> {
    return this.operationAgentService.getStores(merchantId);
  }

  /**
   * 获取店铺详情
   */
  @Get('stores/detail/:storeId')
  @UseGuards(RbacGuard)
  async getStore(@Param('storeId') storeId: string): Promise<Store> {
    return this.operationAgentService.getStore(storeId);
  }

  /**
   * 同步店铺商品
   */
  @Post('stores/:storeId/products/sync')
  @UseGuards(RbacGuard)
  async syncProducts(@Param('storeId') storeId: string): Promise<{ success: boolean; count: number }> {
    return this.operationAgentService.syncProducts(storeId);
  }

  /**
   * 同步店铺订单
   */
  @Post('stores/:storeId/orders/sync')
  @UseGuards(RbacGuard)
  async syncOrders(@Param('storeId') storeId: string): Promise<{ success: boolean; count: number }> {
    return this.operationAgentService.syncOrders(storeId);
  }

  /**
   * 更新商品价格
   */
  @Put('stores/:storeId/products/:productId/price')
  @UseGuards(RbacGuard)
  async updateProductPrice(
    @Param('storeId') storeId: string,
    @Param('productId') productId: string,
    @Body('price') price: number
  ): Promise<boolean> {
    return this.operationAgentService.updateProductPrice(storeId, productId, price);
  }

  /**
   * 停用店铺
   */
  @Put('stores/:storeId/deactivate')
  @UseGuards(RbacGuard)
  async deactivateStore(@Param('storeId') storeId: string): Promise<Store> {
    return this.operationAgentService.deactivateStore(storeId);
  }

  /**
   * 重新激活店铺
   */
  @Put('stores/:storeId/reactivate')
  @UseGuards(RbacGuard)
  async reactivateStore(@Param('storeId') storeId: string): Promise<Store> {
    return this.operationAgentService.reactivateStore(storeId);
  }
}
