import { ApiPropertyOptional } from "@nestjs/swagger";
import { IsBoolean, IsIn, IsInt, IsOptional, IsString, Min } from "class-validator";
import { Transform } from "class-transformer";
import { CompanyKind, CompanyStatus, CompanyType } from "../entities/company.entity";

export class ListCompaniesQueryDto {
  @ApiPropertyOptional({ default: 1 })
  @IsOptional()
  @Transform(({ value }: { value: unknown }) => parseInt(String(value), 10))
  @IsInt()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({ default: 20 })
  @IsOptional()
  @Transform(({ value }: { value: unknown }) => parseInt(String(value), 10))
  @IsInt()
  @Min(1)
  pageSize?: number = 20;

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

  @ApiPropertyOptional({ enum: CompanyType })
  @IsOptional()
  @IsIn(Object.values(CompanyType))
  type?: CompanyType;

  @ApiPropertyOptional({ enum: CompanyKind })
  @IsOptional()
  @IsIn(Object.values(CompanyKind))
  kind?: CompanyKind;

  @ApiPropertyOptional({ enum: CompanyStatus })
  @IsOptional()
  @IsIn(Object.values(CompanyStatus))
  status?: CompanyStatus;

  @ApiPropertyOptional({
    description:
      "Filter by onboarding submission. `true` = reviewable applications; " +
      "`false` = drafts still in the portal wizard. Omit for both.",
  })
  @IsOptional()
  @Transform(({ value }: { value: unknown }) => value === "true" || value === true)
  @IsBoolean()
  onboardingCompleted?: boolean;

  @ApiPropertyOptional({
    description:
      "`true` = only companies with an open (pending) profile change request. " +
      "These are already-approved customers, so they never appear under " +
      "`status=pending` and would otherwise be invisible in the review queue.",
  })
  @IsOptional()
  @Transform(({ value }: { value: unknown }) => value === "true" || value === true)
  @IsBoolean()
  hasPendingChangeRequest?: boolean;

  @ApiPropertyOptional({
    enum: ["review", "name", "createdAt", "updatedAt"],
    default: "review",
    description:
      "Column to order by. The default `review` is a review-queue ordering: " +
      "companies awaiting first approval, then those with a pending change " +
      "request, then everyone else — newest first within each group. The " +
      "other values are plain column sorts.",
  })
  @IsOptional()
  @IsIn(["review", "name", "createdAt", "updatedAt"])
  sortBy?: "review" | "name" | "createdAt" | "updatedAt";

  @ApiPropertyOptional({ enum: ["ASC", "DESC"], default: "DESC" })
  @IsOptional()
  @Transform(({ value }: { value: unknown }) => String(value).toUpperCase())
  @IsIn(["ASC", "DESC"])
  sortOrder?: "ASC" | "DESC";
}
