import { ApiProperty } from '@nestjs/swagger';
import {
  IsBoolean,
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsObject,
  IsOptional,
  IsString,
  MaxLength,
  ValidateIf,
} from 'class-validator';
import { QuestionType } from 'src/common/enum/question-type.enum';
import { AnswerType } from 'src/common/enum/answer-type.enum';

export class CreateMasterQuestionDto {
  @ApiProperty({ description: 'Unique question code', example: 'Q_0000000001' })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  questionCode: string;

  @ApiProperty({ description: 'Binding key for data mapping', example: 'registration.firstName' })
  @IsString()
  @IsNotEmpty()
  @MaxLength(100)
  bindingKey: string;

  @ApiProperty({ description: 'Master form section ID', example: 1 })
  @IsInt()
  @IsNotEmpty()
  masterFormSectionId: number;

  @ApiProperty({ description: 'Question text', example: 'What is your first name?' })
  @IsString()
  @IsNotEmpty()
  questionText: string;

  @ApiProperty({
    description: 'Question type (UI control)',
    enum: QuestionType,
    example: QuestionType.TEXT,
  })
  @IsEnum(QuestionType)
  @IsNotEmpty()
  questionType: QuestionType;

  @ApiProperty({
    description: 'Answer type (data type)',
    enum: AnswerType,
    example: AnswerType.STRING,
  })
  @IsEnum(AnswerType)
  @IsNotEmpty()
  answerType: AnswerType;

  @ApiProperty({ description: 'Answer storage location', required: false, example: 'user.firstName' })
  @IsOptional()
  @IsString()
  @MaxLength(1024)
  answerLocation?: string;

  @ApiProperty({
    description: 'Option configuration for choice-type questions',
    required: false,
    type: 'array',
    items: { type: 'object' },
  })
  @IsOptional()
  @IsObject({ each: true })
  @ValidateIf((o) =>
    [QuestionType.RADIO, QuestionType.CHECKBOX, QuestionType.MULTISELECT, QuestionType.SELECT].includes(
      o.questionType,
    ),
  )
  optionConfig?: Record<string, any>[];

  @ApiProperty({
    description: 'Question configuration (validation, placeholders, etc.)',
    type: Object,
    example: { is_required: true, placeholder: 'Enter your name' },
  })
  @IsObject()
  @IsNotEmpty()
  config: Record<string, any>;

  @ApiProperty({ description: 'Whether question is active', default: true, required: false })
  @IsOptional()
  @IsBoolean()
  isActive?: boolean;

  @ApiProperty({ description: 'User ID who created this question', required: false })
  @IsOptional()
  @IsInt()
  createdBy?: number;

  @ApiProperty({ description: 'User ID who updated this question', required: false })
  @IsOptional()
  @IsInt()
  updatedBy?: number;
}
