import { TaskStatus, TaskPriority } from "@prisma/client";
import {
  IsUUID,
  IsEnum,
  IsOptional,
  IsInt,
  Min,
  Max,
  IsDateString,
  IsString,
} from "class-validator";
import { Type, Transform } from "class-transformer";
import { SortOrder } from "../../common/dto/base-filter.dto";

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

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

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

  @IsOptional()
  @IsUUID("4", { message: "assigneeId must be a valid UUID" })
  assigneeId?: string;

  @IsOptional()
  @IsUUID("4", { message: "projectId must be a valid UUID" })
  projectId?: string;

  @IsOptional()
  @IsUUID("4", { message: "parentId must be a valid UUID" })
  parentId?: string;

  @IsOptional()
  @IsUUID("4", { each: true, message: "domainId must be a valid UUID" })
  @Transform(({ value }) =>
    value === undefined ? undefined : Array.isArray(value) ? value : [value]
  )
  domainId?: string | string[];

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

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

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

  @IsOptional()
  @IsDateString({}, { message: "dueDateFrom must be a valid ISO 8601 date string" })
  dueDateFrom?: Date;

  @IsOptional()
  @IsDateString({}, { message: "dueDateTo must be a valid ISO 8601 date string" })
  dueDateTo?: Date;

  @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;
}
