import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
  IsArray,
  IsBoolean,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';
import { DateRangeDto } from 'src/utils/datetime/dateRangeDto';
import { PaginationDto } from 'src/config/pagination/paginationDto';

/**
 * DTO for filtering and searching Visa Commission records.
 *
 * Extends PaginationDto to include pagination fields (page, limit, etc.).
 * All fields are optional filters for querying visa commission data.
 */
export class SearchVisaCommissionDto extends PaginationDto {
  /**
   * Search keyword for filtering commission records by title or description.
   */
  @ApiPropertyOptional({ description: 'Search by title or description' })
  @IsOptional()
  @IsString()
  search_title?: string;

  /**
   * Filter by type of association (e.g., institute, sub-agent, etc.)
   */
  @ApiPropertyOptional({ description: 'Type of association' })
  @IsOptional()
  @IsString()
  association_type?: string;

  /**
   * Filter by product IDs. Can pass multiple product IDs for multi-filter.
   */
  @ApiPropertyOptional({ type: [String], description: 'Filter by product IDs' })
  @IsOptional()
  @IsArray()
  product_id?: string[];

  /**
   * Filter by visa service country IDs.
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Visa service country IDs',
  })
  @IsOptional()
  @IsArray()
  visa_country_id?: string[];

  /**
   * Filter by client originating country IDs.
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Client originating country IDs',
  })
  @IsOptional()
  @IsArray()
  visa_client_country_id?: string[];

  @ApiPropertyOptional({
    description: 'Filter by institute IDs',
    type: [String],
  })
  @IsOptional()
  @IsArray()
  @IsString({ each: true }) // Validates each element is a string
  @Type(() => String) // Explicitly tells class-transformer to handle as string array
  institute_id?: string[];

  /**
   * Filter by users who created the record.
   */
  @ApiPropertyOptional({ type: [String], description: 'Created by user IDs' })
  @IsOptional()
  @IsArray()
  created_by?: string[];

  /**
   * Filter by users who updated the record.
   */
  @ApiPropertyOptional({ type: [String], description: 'Updated by user IDs' })
  @IsOptional()
  @IsArray()
  updated_by?: string[];

  /**
   * Filter by student condition values (e.g., 1, 2)
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Student condition values',
  })
  @IsOptional()
  @IsArray()
  student_condition?: string[];

  /**
   * Filter by currency codes.
   */
  @ApiPropertyOptional({ type: [String], description: 'Currency codes' })
  @IsOptional()
  @IsArray()
  currency?: string[];

  /**
   * Filter by campus condition flags.
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Campus condition flags',
  })
  @IsOptional()
  @IsArray()
  campus_condition?: string[];

  /**
   * Filter by study level condition flags.
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Study level condition flags',
  })
  @IsOptional()
  @IsArray()
  study_level_condition?: string[];

  /**
   * Filter by course condition flags.
   */
  @ApiPropertyOptional({
    type: [String],
    description: 'Course condition flags',
  })
  @IsOptional()
  @IsArray()
  course_condition?: string[];

  /**
   * Filter by active status (e.g., active/inactive)
   */
  @ApiPropertyOptional({ type: [String], description: 'Active status values' })
  @IsOptional()
  @IsArray()
  active?: string[];

  /**
   * Filter by specific associate IDs.
   */
  @ApiPropertyOptional({ type: [String], description: 'Associate IDs' })
  @IsOptional()
  @IsArray()
  associate_id?: string[];

  /**
   * Flag to export the results in CSV format.
   */
  @ApiPropertyOptional({ description: 'Export as CSV', default: false })
  @IsOptional()
  @IsBoolean()
  is_csv?: boolean = false;

  /**
   * Filter by creation date range.
   */
  @ApiPropertyOptional({
    type: DateRangeDto,
    description: 'Filter by creation date range',
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => DateRangeDto)
  created_on?: DateRangeDto;

  /**
   * Filter by last updated date range.
   */
  @ApiPropertyOptional({
    type: DateRangeDto,
    description: 'Filter by last updated date range',
  })
  @IsOptional()
  @ValidateNested()
  @Type(() => DateRangeDto)
  updated_on?: DateRangeDto;
}
