import { IsString, IsNotEmpty, IsOptional, IsInt, IsEnum, IsArray, IsDateString, Min } from 'class-validator';

export enum TaskType {
  READING = 'READING',
  ACTIVITY = 'ACTIVITY',
  HOMEWORK = 'HOMEWORK',
}

export enum TargetType {
  CLASS = 'CLASS',
  STUDENT = 'STUDENT',
}

export enum TaskStatus {
  DRAFT = 'DRAFT',
  PUBLISHED = 'PUBLISHED',
  ARCHIVED = 'ARCHIVED',
}

export enum CompletionStatus {
  PENDING = 'PENDING',
  IN_PROGRESS = 'IN_PROGRESS',
  COMPLETED = 'COMPLETED',
}

export class CreateTaskDto {
  @IsString()
  @IsNotEmpty({ message: '任务标题不能为空' })
  title: string;

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

  @IsEnum(TaskType)
  taskType: TaskType;

  @IsEnum(TargetType)
  targetType: TargetType;

  @IsOptional()
  @IsInt()
  relatedCourseId?: number;

  @IsDateString()
  startDate: string;

  @IsDateString()
  endDate: string;

  @IsArray()
  @IsInt({ each: true })
  targetIds: number[]; // classIds or studentIds based on targetType
}

export class UpdateTaskDto {
  @IsOptional()
  @IsString()
  @IsNotEmpty({ message: '任务标题不能为空' })
  title?: string;

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

  @IsOptional()
  @IsDateString()
  startDate?: string;

  @IsOptional()
  @IsDateString()
  endDate?: string;

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

  @IsOptional()
  @IsArray()
  @IsInt({ each: true })
  targetIds?: number[];
}

export class UpdateCompletionDto {
  @IsEnum(CompletionStatus)
  status: CompletionStatus;

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

  @IsOptional()
  @IsString()
  parentFeedback?: string;
}

export class QueryTaskDto {
  @IsOptional()
  @IsInt()
  page?: number;

  @IsOptional()
  @IsInt()
  pageSize?: number;

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

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

  @IsOptional()
  @IsString()
  keyword?: string;
}

// ==================== 任务模板 DTO ====================

export class CreateTaskTemplateDto {
  @IsString()
  @IsNotEmpty({ message: '模板名称不能为空' })
  name: string;

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

  @IsEnum(TaskType)
  taskType: TaskType;

  @IsOptional()
  @IsInt()
  relatedCourseId?: number;

  @IsOptional()
  @IsInt()
  @Min(1)
  defaultDuration?: number;

  @IsOptional()
  isDefault?: boolean;
}

export class UpdateTaskTemplateDto {
  @IsOptional()
  @IsString()
  @IsNotEmpty({ message: '模板名称不能为空' })
  name?: string;

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

  @IsOptional()
  @IsInt()
  relatedCourseId?: number;

  @IsOptional()
  @IsInt()
  @Min(1)
  defaultDuration?: number;

  @IsOptional()
  isDefault?: boolean;

  @IsOptional()
  @IsString()
  status?: string;
}

export class CreateFromTemplateDto {
  @IsInt()
  templateId: number;

  @IsArray()
  @IsInt({ each: true })
  targetIds: number[];

  @IsString()
  targetType: string;

  @IsOptional()
  @IsDateString()
  startDate?: string;
}
