import { ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean, IsEnum, IsInt, IsOptional, IsString, IsUUID, Min } from 'class-validator';

import {
  WAREHOUSE_INVENTORY_STATUSES,
  WarehouseInventoryStatus,
} from '../entities/warehouse-inventory.entity';

export class FilterWarehouseInventoryDto {
  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  warehouseId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  facilityId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  yardId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  zoneId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  bookingId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  cargoId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  containerId?: string;

  @ApiPropertyOptional({ format: 'uuid' })
  @IsOptional()
  @IsUUID()
  goodsId?: string;

  @ApiPropertyOptional({ enum: WAREHOUSE_INVENTORY_STATUSES })
  @IsOptional()
  @IsEnum(WAREHOUSE_INVENTORY_STATUSES)
  status?: WarehouseInventoryStatus;

  @ApiPropertyOptional({ enum: ['IMPORT', 'EXPORT'] })
  @IsOptional()
  @IsEnum(['IMPORT', 'EXPORT'])
  direction?: 'IMPORT' | 'EXPORT';

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  search?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  dateFrom?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  dateTo?: string;

  // ── KPI drill-down filters ──────────────────────────────────────────────
  // Each mirrors one opsStats() counter so a dashboard card's count always
  // equals the length of the list it opens.

  @ApiPropertyOptional({ type: Boolean, description: 'Only items received (created) today' })
  @IsOptional()
  @Transform(({ value }) => (value == null ? undefined : value === true || value === 'true' || value === '1'))
  @IsBoolean()
  receivedToday?: boolean;

  @ApiPropertyOptional({ type: Boolean, description: 'Only RECEIVED items with no inspection yet' })
  @IsOptional()
  @Transform(({ value }) => (value == null ? undefined : value === true || value === 'true' || value === '1'))
  @IsBoolean()
  pendingInspection?: boolean;

  @ApiPropertyOptional({ type: Number, minimum: 1, description: 'Only in-warehouse items older than N days' })
  @IsOptional()
  @Transform(({ value }) => (value === '' || value == null ? undefined : Number(value)))
  @IsInt()
  @Min(1)
  agingOverDays?: number;
}
