import { Transform } from 'class-transformer'
import { IsDateString, IsEnum, IsInt, IsOptional, IsString, Max, MaxLength, Min } from 'class-validator'

// Query params for GET /audit. All optional — empty returns the most recent
// page. Pagination is cursor-based via `before`, which is the ISO timestamp of
// the last event in the previous page.
export class ListAuditDto {
  @IsOptional() @IsDateString()
  since?: string

  @IsOptional() @IsDateString()
  until?: string

  // ISO timestamp from the last event in the previous page. Mongo query is
  // `{ at: { $lt: before } }` so the next page picks up immediately after.
  @IsOptional() @IsDateString()
  before?: string

  // dotted.snake_case verb or prefix (e.g. 'tenant' matches everything under
  // 'tenant.*'). Server uses a prefix match against the indexed `action` field.
  @IsOptional() @IsString() @MaxLength(80)
  action?: string

  @IsOptional() @IsString() @MaxLength(64)
  tenantSlug?: string

  @IsOptional() @IsString() @MaxLength(64)
  partnerSlug?: string

  @IsOptional() @IsString() @MaxLength(120)
  actorEmail?: string

  @IsOptional() @IsEnum(['success', 'failure'])
  outcome?: 'success' | 'failure'

  @IsOptional() @IsString() @MaxLength(80)
  q?: string  // free-text on action / resourceName / actorEmail

  @IsOptional()
  @Transform(({ value }) => (value === undefined ? undefined : Number(value)))
  @IsInt() @Min(1) @Max(500)
  limit?: number
}
