import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Query,
  UseGuards,
} 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 { InventoryService } from './services/inventory.service';
import { Auditable, Sensitive, Financial } from '@core/observability/audit/decorators/auditable.decorator';
import {
  CheckInDto,
  CheckOutDto,
  AdjustInventoryDto,
  TransferInventoryDto,
  QueryInventoryLogsDto,
  InventoryStatsDto,
  BulkInventoryOperationDto,
} from './dto/inventory.dto';

@Controller('inventory')
export class InventoryController {
  constructor(private readonly inventoryService: InventoryService) {}

  /**
   * 扫码入库 (Check-in)
   */
  @Post('check-in')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:checkin')
  async checkIn(@Body() checkInDto: CheckInDto, @CurrentUser() user: CurrentUserPayload) {
    return this.inventoryService.checkIn(
      checkInDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 扫码出库 (Check-out)
   */
  @Post('check-out')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:checkout')
  async checkOut(@Body() checkOutDto: CheckOutDto, @CurrentUser() user: CurrentUserPayload) {
    return this.inventoryService.checkOut(
      checkOutDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 库存调整
   */
  @Post('adjust')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:adjust')
  async adjustInventory(
    @Body() adjustDto: AdjustInventoryDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    return this.inventoryService.adjustInventory(
      adjustDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 库存转移
   */
  @Post('transfer')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:transfer')
  async transferInventory(
    @Body() transferDto: TransferInventoryDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    return this.inventoryService.transferInventory(
      transferDto,
      user.userId,
      user.username,
    );
  }

  /**
   * 查询库存日志
   */
  @Get('logs')
  @RequirePermissions('parts:read')
  async findLogs(@Query() query: QueryInventoryLogsDto) {
    return this.inventoryService.findLogs(query);
  }

  /**
   * 获取零件的库存历史
   */
  @Get('history/:partId')
  @RequirePermissions('parts:read')
  async getPartInventoryHistory(
    @Param('partId') partId: string,
    @Query('days') days?: number,
  ) {
    return this.inventoryService.getPartInventoryHistory(
      partId,
      days ? parseInt(days.toString()) : 30,
    );
  }

  /**
   * 获取库存统计
   */
  @Get('stats')
  @RequirePermissions('parts:read')
  async getStats(@Query() query: InventoryStatsDto) {
    return this.inventoryService.getStats(query);
  }

  /**
   * 批量库存操作
   */
  @Post('bulk-operation')
  @Auditable()
  @Sensitive()
  @RequirePermissions('parts:adjust')
  async bulkOperation(
    @Body() bulkDto: BulkInventoryOperationDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    return this.inventoryService.bulkOperation(
      bulkDto,
      user.userId,
      user.username,
    );
  }
}

