import {
  Body,
  Controller,
  Delete,
  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 { CatalogService } from './catalog.service';
import type {
  CatalogListQuery,
  CreateCatalogCategoryDto,
  CreateCatalogProductDto,
  UpdateCatalogCategoryDto,
  UpdateCatalogProductDto,
  UpdateEnabledDto,
} from './dto/catalog.dto';

@UseGuards(AuthGuard)
@Controller('api/admin/catalog')
export class CatalogController {
  constructor(private readonly catalogService: CatalogService) {}

  @Get('categories')
  listCategories(
    @Req() request: AuthenticatedRequest,
    @Query() query: CatalogListQuery,
  ) {
    return this.catalogService.listCategories(request.user, query);
  }

  @Post('categories')
  @OperationLog({
    action: 'catalog.categories.create',
    resourceType: 'category',
    resourceIdFromResult: 'id',
    metadataFromBody: ['name', 'sortOrder', 'enabled'],
    description: '新建自营分类',
  })
  createCategory(
    @Req() request: AuthenticatedRequest,
    @Body() dto: CreateCatalogCategoryDto,
  ) {
    return this.catalogService.createCategory(request.user, dto);
  }

  @Patch('categories/:id')
  @OperationLog({
    action: 'catalog.categories.update',
    resourceType: 'category',
    resourceIdParam: 'id',
    metadataFromBody: ['name', 'sortOrder', 'enabled'],
    description: '编辑自营分类',
  })
  updateCategory(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
    @Body() dto: UpdateCatalogCategoryDto,
  ) {
    return this.catalogService.updateCategory(request.user, id, dto);
  }

  @Patch('categories/:id/enabled')
  @OperationLog({
    action: 'catalog.categories.update_enabled',
    resourceType: 'category',
    resourceIdParam: 'id',
    metadataFromBody: ['enabled'],
    description: '更新分类启停状态',
  })
  updateCategoryEnabled(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
    @Body() dto: UpdateEnabledDto,
  ) {
    return this.catalogService.updateCategoryEnabled(request.user, id, dto);
  }

  @Delete('categories/:id')
  @OperationLog({
    action: 'catalog.categories.delete',
    resourceType: 'category',
    resourceIdParam: 'id',
    description: '删除自营分类',
  })
  deleteCategory(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
  ) {
    return this.catalogService.deleteCategory(request.user, id);
  }

  @Post('categories/sync-third-party')
  @OperationLog({
    action: 'catalog.categories.sync_third_party',
    resourceType: 'category',
    description: '同步第三方分类',
  })
  syncThirdPartyCategories(@Req() request: AuthenticatedRequest) {
    return this.catalogService.syncThirdPartyCategories(request.user);
  }

  @Get('sync-jobs')
  listSyncJobs(
    @Req() request: AuthenticatedRequest,
    @Query() query: CatalogListQuery,
  ) {
    return this.catalogService.listSyncJobs(request.user, query);
  }

  @Get('products')
  listProducts(
    @Req() request: AuthenticatedRequest,
    @Query() query: CatalogListQuery,
  ) {
    return this.catalogService.listProducts(request.user, query);
  }

  @Post('products')
  @OperationLog({
    action: 'catalog.products.create',
    resourceType: 'product',
    resourceIdFromResult: 'id',
    metadataFromBody: ['name', 'categoryId', 'price', 'fulfillmentType', 'enabled'],
    description: '新建自营商品',
  })
  createProduct(
    @Req() request: AuthenticatedRequest,
    @Body() dto: CreateCatalogProductDto,
  ) {
    return this.catalogService.createProduct(request.user, dto);
  }

  @Patch('products/:id')
  @OperationLog({
    action: 'catalog.products.update',
    resourceType: 'product',
    resourceIdParam: 'id',
    metadataFromBody: ['name', 'categoryId', 'price', 'fulfillmentType', 'enabled'],
    description: '编辑自营商品',
  })
  updateProduct(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
    @Body() dto: UpdateCatalogProductDto,
  ) {
    return this.catalogService.updateProduct(request.user, id, dto);
  }

  @Patch('products/:id/enabled')
  @OperationLog({
    action: 'catalog.products.update_enabled',
    resourceType: 'product',
    resourceIdParam: 'id',
    metadataFromBody: ['enabled'],
    description: '更新商品启停状态',
  })
  updateProductEnabled(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
    @Body() dto: UpdateEnabledDto,
  ) {
    return this.catalogService.updateProductEnabled(request.user, id, dto);
  }

  @Delete('products/:id')
  @OperationLog({
    action: 'catalog.products.delete',
    resourceType: 'product',
    resourceIdParam: 'id',
    description: '删除商品',
  })
  deleteProduct(
    @Req() request: AuthenticatedRequest,
    @Param('id') id: string,
  ) {
    return this.catalogService.deleteProduct(request.user, id);
  }

  @Post('products/sync-third-party')
  @OperationLog({
    action: 'catalog.products.sync_third_party',
    resourceType: 'product',
    metadataFromQuery: ['remoteCategoryId'],
    description: '同步第三方商品',
  })
  syncThirdPartyProducts(
    @Req() request: AuthenticatedRequest,
    @Query() query: CatalogListQuery,
  ) {
    return this.catalogService.syncThirdPartyProducts(request.user, query);
  }
}
