import { CredentialType, CredentialScope } from "@prisma/client";
import { IsEnum, IsOptional, IsInt, Min, Max, IsString, IsBoolean, IsUUID } from "class-validator";
import { Type, Transform } from "class-transformer";

/**
 * DTO for querying credentials with filters and pagination
 */
export class QueryCredentialDto {
  @IsOptional()
  @IsUUID("4", { message: "workspaceId must be a valid UUID" })
  workspaceId?: string;

  @IsOptional()
  @IsEnum(CredentialType, { each: true, message: "type must be a valid CredentialType" })
  @Transform(({ value }) =>
    value === undefined ? undefined : Array.isArray(value) ? value : [value]
  )
  type?: CredentialType | CredentialType[];

  @IsOptional()
  @IsEnum(CredentialScope, { message: "scope must be a valid CredentialScope" })
  scope?: CredentialScope;

  @IsOptional()
  @IsString({ message: "provider must be a string" })
  provider?: string;

  @IsOptional()
  @IsString({ message: "search must be a string" })
  search?: string;

  @IsOptional()
  @IsBoolean({ message: "isActive must be a boolean" })
  @Transform(({ value }) => value === "true" || value === true)
  isActive?: boolean;

  @IsOptional()
  @Type(() => Number)
  @IsInt({ message: "page must be an integer" })
  @Min(1, { message: "page must be at least 1" })
  page?: number;

  @IsOptional()
  @Type(() => Number)
  @IsInt({ message: "limit must be an integer" })
  @Min(1, { message: "limit must be at least 1" })
  @Max(100, { message: "limit must not exceed 100" })
  limit?: number;
}
