import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  UseGuards,
  HttpCode,
  HttpStatus,
  Query,
} from '@nestjs/common';
import { PermissionsGuard } from '@modules/organization/auth/guards/permissions.guard';
import { RequirePermissions } from '@common/decorators/permissions.decorator';
import { CurrentUser } from '@common/decorators/current-user.decorator';
import type { CurrentUserPayload } from '@common/decorators/current-user.decorator';
import { LabelService } from './services/label.service';
import { Auditable, Sensitive, Financial } from '@core/observability/audit/decorators/auditable.decorator';
import {
  GenerateLabelDto,
  BulkGenerateLabelsDto,
  PrintLabelDto,
  BulkPrintLabelsDto,
} from './dto/part.dto';

@Controller('labels')
export class LabelsController {
  constructor(private readonly labelService: LabelService) {}

  /**
   * 生成标签
   */
  @Post('generate')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:label')
  async generateLabel(@Body() generateDto: GenerateLabelDto, @CurrentUser() user: CurrentUserPayload) {
    return this.labelService.generateLabel(
      generateDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 批量生成标签
   */
  @Post('generate/bulk')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:label')
  async bulkGenerateLabels(
    @Body() bulkDto: BulkGenerateLabelsDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    return this.labelService.bulkGenerateLabels(
      bulkDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 打印标签
   */
  @Post('print')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:label')
  async printLabel(@Body() printDto: PrintLabelDto, @CurrentUser() user: CurrentUserPayload) {
    return this.labelService.printLabel(
      printDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 批量打印标签
   */
  @Post('print/bulk')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:label')
  async bulkPrintLabels(
    @Body() bulkDto: BulkPrintLabelsDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    return this.labelService.bulkPrintLabels(
      bulkDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 获取标签详情
   */
  @Get(':id')
  @RequirePermissions('parts:read')
  async findOne(@Param('id') id: string) {
    return this.labelService.findOne(id);
  }

  /**
   * 通过 Label Code 查询标签
   */
  @Get('by-code/:labelCode')
  @RequirePermissions('parts:read')
  async findByLabelCode(@Param('labelCode') labelCode: string) {
    return this.labelService.findByLabelCode(labelCode);
  }

  /**
   * 获取零件的所有标签
   */
  @Get('by-part/:partId')
  @RequirePermissions('parts:read')
  async findByPartId(@Param('partId') partId: string) {
    return this.labelService.findByPartId(partId);
  }

  /**
   * 作废标签
   */
  @Delete(':id')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:label')
  @HttpCode(HttpStatus.NO_CONTENT)
  async voidLabel(@Param('id') id: string, @CurrentUser() user: CurrentUserPayload) {
    return this.labelService.voidLabel(id, user.userId);
  }

  /**
   * 获取打印统计
   */
  @Get('stats/print')
  @RequirePermissions('parts:read')
  async getPrintStats(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    return this.labelService.getPrintStats(
      startDate ? new Date(startDate) : undefined,
      endDate ? new Date(endDate) : undefined,
    );
  }
}

