import {
  IsEnum,
  IsInt,
  IsOptional,
  IsString,
  Matches,
  Max,
  Min,
} from 'class-validator';
import { Type } from 'class-transformer';

import { ClientGroup, LeadStatus } from '../entities/lead.entity';

export class ListLeadsQueryDto {
  @IsOptional()
  @IsString()
  search?: string;

  @IsOptional()
  @IsEnum(LeadStatus)
  status?: LeadStatus;

  /**
   * 🆕 Accepts all four stored values. The service expands the filter, so
   * ?client_group=QRS matches both QRS and legacy QRS_B rows.
   */
  @IsOptional()
  @IsEnum(ClientGroup)
  client_group?: ClientGroup;

  @IsOptional()
  @IsInt()
  @Type(() => Number)
  assigned_to?: number;

  @IsOptional()
  @IsString()
  source?: string;

  @IsOptional()
  @IsString()
  tag?: string;

  @IsOptional()
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/, { message: 'date_from must be YYYY-MM-DD' })
  date_from?: string;

  @IsOptional()
  @IsString()
  @Matches(/^\d{4}-\d{2}-\d{2}$/, { message: 'date_to must be YYYY-MM-DD' })
  date_to?: string;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Type(() => Number)
  page?: number;

  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  @Type(() => Number)
  limit?: number;
}

/** Query DTO for GET /leads/kanban and /leads/kanban/load-more */
export class KanbanQueryDto {
  @IsOptional()
  @IsInt()
  @Type(() => Number)
  assigned_to?: number;

  @IsOptional()
  @IsEnum(ClientGroup)
  client_group?: ClientGroup;
}

export class KanbanLoadMoreQueryDto extends KanbanQueryDto {
  @IsEnum(LeadStatus)
  status: LeadStatus;

  @IsOptional()
  @IsInt()
  @Min(0)
  @Type(() => Number)
  offset?: number;
}
