import {
  IsString,
  IsOptional,
  IsNotEmpty,
  IsInt,
  IsObject,
  IsDateString,
  IsEnum,
  Min,
  MaxLength,
  IsArray,
} from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';

/**
 * 操作状态枚举
 */
export enum PendingActionStatus {
  PENDING = 'PENDING',
  COMPLETED = 'COMPLETED',
  CANCELLED = 'CANCELLED',
}

/**
 * 创建用户待办操作 DTO (后台管理)
 */
export class CreatePendingActionDto {
  @ApiProperty({ example: '12345', description: '目标用户ID' })
  @IsString()
  @IsNotEmpty({ message: '用户ID不能为空' })
  userId: string;

  @ApiProperty({
    example: 'ADOPTION_WIZARD',
    description: '操作代码，如: ADOPTION_WIZARD, SETTLE_REWARDS, BIND_PHONE, FORCE_KYC',
  })
  @IsString()
  @IsNotEmpty({ message: '操作代码不能为空' })
  @MaxLength(50)
  actionCode: string;

  @ApiPropertyOptional({
    example: { treeType: 'durian', location: '马来西亚' },
    description: '操作参数 (JSON对象)',
  })
  @IsOptional()
  @IsObject()
  actionParams?: Record<string, any>;

  @ApiPropertyOptional({ example: 10, description: '优先级 (数值越大优先级越高)' })
  @IsOptional()
  @IsInt()
  @Min(0)
  priority?: number;

  @ApiPropertyOptional({
    example: '2025-12-31T23:59:59Z',
    description: '过期时间 (ISO 8601格式)',
  })
  @IsOptional()
  @IsDateString()
  expiresAt?: string;
}

/**
 * 批量创建用户待办操作 DTO
 */
export class BatchCreatePendingActionDto {
  @ApiProperty({ type: [String], description: '目标用户ID列表' })
  @IsArray()
  @IsString({ each: true })
  @IsNotEmpty({ each: true })
  userIds: string[];

  @ApiProperty({ example: 'ADOPTION_WIZARD', description: '操作代码' })
  @IsString()
  @IsNotEmpty({ message: '操作代码不能为空' })
  @MaxLength(50)
  actionCode: string;

  @ApiPropertyOptional({ description: '操作参数' })
  @IsOptional()
  @IsObject()
  actionParams?: Record<string, any>;

  @ApiPropertyOptional({ example: 0, description: '优先级' })
  @IsOptional()
  @IsInt()
  @Min(0)
  priority?: number;

  @ApiPropertyOptional({ description: '过期时间' })
  @IsOptional()
  @IsDateString()
  expiresAt?: string;
}

/**
 * 更新用户待办操作 DTO
 */
export class UpdatePendingActionDto {
  @ApiPropertyOptional({ description: '操作参数' })
  @IsOptional()
  @IsObject()
  actionParams?: Record<string, any>;

  @ApiPropertyOptional({ enum: PendingActionStatus, description: '状态' })
  @IsOptional()
  @IsEnum(PendingActionStatus)
  status?: PendingActionStatus;

  @ApiPropertyOptional({ description: '优先级' })
  @IsOptional()
  @IsInt()
  @Min(0)
  priority?: number;

  @ApiPropertyOptional({ description: '过期时间' })
  @IsOptional()
  @IsDateString()
  expiresAt?: string;
}

/**
 * 查询用户待办操作 DTO (后台管理)
 */
export class QueryPendingActionsDto {
  @ApiPropertyOptional({ description: '用户ID' })
  @IsOptional()
  @IsString()
  userId?: string;

  @ApiPropertyOptional({ description: '操作代码' })
  @IsOptional()
  @IsString()
  actionCode?: string;

  @ApiPropertyOptional({ enum: PendingActionStatus, description: '状态' })
  @IsOptional()
  @IsEnum(PendingActionStatus)
  status?: PendingActionStatus;

  @ApiPropertyOptional({ example: 1, description: '页码' })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  page?: number;

  @ApiPropertyOptional({ example: 20, description: '每页数量' })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  limit?: number;
}

/**
 * 用户标记操作完成 DTO
 */
export class CompletePendingActionDto {
  @ApiPropertyOptional({ description: '完成时附带的数据' })
  @IsOptional()
  @IsObject()
  resultData?: Record<string, any>;
}
