import { ApiProperty } from '@nestjs/swagger';
import {
  IsArray,
  IsEnum,
  IsInt,
  IsNotEmpty,
  IsObject,
  IsOptional,
  IsString,
  ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { QuestionType } from 'src/common/enum/question-type.enum';
import { AnswerType } from 'src/common/enum/answer-type.enum';
import { QuestionOverrideDto } from 'src/template-form-section/dto/template-form-builder.dto';

/**
 * DTO for updating individual questions in template section
 */
export class TemplateFormUpdateQuestionDto {
  @ApiProperty({
    description: 'Template question ID to update',
    example: 12,
  })
  @IsInt()
  @IsNotEmpty()
  templateQuestionId: number;

  @ApiProperty({
    description: 'Question text/label',
    example: 'What is your preferred contact method?',
    required: false,
  })
  @IsOptional()
  @IsString()
  questionText?: string;

  @ApiProperty({
    description: 'Question type',
    enum: QuestionType,
    example: QuestionType.RADIO,
    required: false,
  })
  @IsOptional()
  @IsEnum(QuestionType)
  questionType?: QuestionType;

  @ApiProperty({
    description: 'Answer type',
    enum: AnswerType,
    example: AnswerType.STRING,
    required: false,
  })
  @IsOptional()
  @IsEnum(AnswerType)
  answerType?: AnswerType;

  @ApiProperty({
    description: 'Question configuration (validation rules, conditional logic, etc.)',
    example: { required: true, maxLength: 100 },
    required: false,
  })
  @IsOptional()
  @IsObject()
  config?: Record<string, any>;

  @ApiProperty({
    description: 'Option configuration for select/multi-select questions',
    example: [{ label: 'Email', value: 'email' }, { label: 'Phone', value: 'phone' }],
    required: false,
  })
  @IsOptional()
  @IsArray()
  optionConfig?: Record<string, any>[];

  @ApiProperty({
    description: 'Conditional configuration for program/field-based conditions',
    required: false,
    type: Object,
  })
  @IsOptional()
  @IsObject()
  conditionalConfig?: Record<string, any>;

  @ApiProperty({
    description: 'Display order within section',
    example: 5,
    required: false,
  })
  @IsOptional()
  @IsInt()
  displayOrder?: number;
}

/**
 * DTO for adding new questions to template section
 */
export class AddTemplateQuestionDto {
  @ApiProperty({
    description: 'Master question ID to clone from',
    example: 42,
  })
  @IsInt()
  @IsNotEmpty()
  masterQuestionId: number;

  @ApiProperty({
    description: 'Override display order (optional)',
    example: 10,
    required: false,
  })
  @IsOptional()
  @IsInt()
  displayOrder?: number;

  @ApiProperty({
    description: 'Overrides to apply when cloning from master',
    type: QuestionOverrideDto,
    required: false,
    example: {
      label: 'Custom Question Label',
      placeholder: 'Enter your response',
      config: { isRequired: true, maxCharacters: 100 },
      optionConfig: [{ value: 'yes', label: 'Yes' }, { value: 'no', label: 'No' }]
    }
  })
  @IsOptional()
  @IsObject()
  @ValidateNested()
  @Type(() => QuestionOverrideDto)
  override?: QuestionOverrideDto;
}

/**
 * DTO for updating a template section
 */
export class UpdateTemplateSectionDto {
  @ApiProperty({
    description: 'Template form section ID to update',
    example: 25,
  })
  @IsInt()
  @IsNotEmpty()
  templateFormSectionId: number;

  @ApiProperty({
    description: 'Section name (for rename)',
    example: 'Updated Personal Information',
    required: false,
  })
  @IsOptional()
  @IsString()
  name?: string;

  @ApiProperty({
    description: 'Section description',
    example: 'Updated description for personal info section',
    required: false,
  })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiProperty({
    description: 'Display order',
    example: 3,
    required: false,
  })
  @IsOptional()
  @IsInt()
  displayOrder?: number;

  @ApiProperty({
    description: 'Conditional configuration',
    example: { showIf: { field: 'isInternational', value: 'yes' } },
    required: false,
  })
  @IsOptional()
  @IsObject()
  conditionalConfig?: Record<string, any>;

  @ApiProperty({
    description: 'Questions to update in this section',
    type: [TemplateFormUpdateQuestionDto],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => TemplateFormUpdateQuestionDto)
  updateQuestions?: TemplateFormUpdateQuestionDto[];

  @ApiProperty({
    description: 'New questions to add to this section',
    type: [AddTemplateQuestionDto],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => AddTemplateQuestionDto)
  addQuestions?: AddTemplateQuestionDto[];

  @ApiProperty({
    description: 'Template question IDs to delete from this section',
    type: [Number],
    example: [101, 102, 103],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @IsInt({ each: true })
  deleteQuestionIds?: number[];
}

/**
 * DTO for adding new sections to template
 */
export class AddTemplateSectionDto {
  @ApiProperty({
    description: 'Master form section ID to clone from (optional)',
    example: 5,
    required: false,
  })
  @IsOptional()
  @IsInt()
  masterFormSectionId?: number;

  @ApiProperty({
    description: 'Section key (unique identifier)',
    example: 'FS_EMERGENCY_CONTACT',
  })
  @IsString()
  @IsNotEmpty()
  sectionKey: string;

  @ApiProperty({
    description: 'Section name',
    example: 'Emergency Contact Information',
  })
  @IsString()
  @IsNotEmpty()
  name: string;

  @ApiProperty({
    description: 'Section description',
    example: 'Provide emergency contact details',
    required: false,
  })
  @IsOptional()
  @IsString()
  description?: string;

  @ApiProperty({
    description: 'Parent section ID (for nested sections)',
    example: 20,
    required: false,
  })
  @IsOptional()
  @IsInt()
  parentSectionId?: number;

  @ApiProperty({
    description: 'Conditional configuration',
    example: { showIf: { field: 'needsContact', value: true } },
    required: false,
  })
  @IsOptional()
  @IsObject()
  conditionalConfig?: Record<string, any>;

  @ApiProperty({
    description: 'Display order',
    example: 8,
  })
  @IsInt()
  @IsNotEmpty()
  displayOrder: number;

  @ApiProperty({
    description: 'Questions to add to this new section',
    type: [AddTemplateQuestionDto],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => AddTemplateQuestionDto)
  questions?: AddTemplateQuestionDto[];
}

/**
 * Main DTO for updating template form structure
 */
export class UpdateTemplateFormDto {
  @ApiProperty({
    description: 'Program template ID',
    example: 5,
  })
  @IsInt()
  @IsNotEmpty()
  programTemplateId: number;

  @ApiProperty({
    description: 'Template form section IDs to delete (entire sections)',
    type: [Number],
    example: [15, 16],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @IsInt({ each: true })
  deleteSectionIds?: number[];

  @ApiProperty({
    description: 'Sections to update (with questions)',
    type: [UpdateTemplateSectionDto],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => UpdateTemplateSectionDto)
  updateSections?: UpdateTemplateSectionDto[];

  @ApiProperty({
    description: 'New sections to add to template',
    type: [AddTemplateSectionDto],
    required: false,
  })
  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => AddTemplateSectionDto)
  addSections?: AddTemplateSectionDto[];
}
