import { IsString, IsNumber, Min, Max, IsOptional, IsEnum } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { WalletSessionStatus } from '../../../domain/enums/wallet-session-status.enum';

/**
 * 创建共管钱包会话 DTO
 */
export class CreateCoManagedWalletSessionDto {
  @ApiProperty({ description: '钱包名称' })
  @IsString()
  walletName: string;

  @ApiProperty({ description: '签名阈值 (T)', minimum: 1, maximum: 10 })
  @IsNumber()
  @Min(1)
  @Max(10)
  thresholdT: number;

  @ApiProperty({ description: '参与方总数 (N)', minimum: 2, maximum: 10 })
  @IsNumber()
  @Min(2)
  @Max(10)
  thresholdN: number;

  @ApiProperty({ description: '发起者名称' })
  @IsString()
  initiatorName: string;
}

/**
 * 加入会话 DTO
 */
export class JoinSessionDto {
  @ApiProperty({ description: '参与者名称' })
  @IsString()
  participantName: string;

  @ApiProperty({ description: '参与方 ID' })
  @IsString()
  partyId: string;
}

/**
 * 验证邀请码 DTO
 */
export class ValidateInviteCodeDto {
  @ApiProperty({ description: '邀请码' })
  @IsString()
  inviteCode: string;
}

/**
 * 查询会话列表 DTO
 */
export class ListSessionsDto {
  @ApiPropertyOptional({ description: '页码', default: 1 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({ description: '每页数量', default: 10 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  @IsNumber()
  @Min(1)
  @Max(100)
  pageSize?: number = 10;

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

/**
 * 查询钱包列表 DTO
 */
export class ListWalletsDto {
  @ApiPropertyOptional({ description: '页码', default: 1 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  @IsNumber()
  @Min(1)
  page?: number = 1;

  @ApiPropertyOptional({ description: '每页数量', default: 10 })
  @IsOptional()
  @Transform(({ value }) => parseInt(value, 10))
  @IsNumber()
  @Min(1)
  @Max(100)
  pageSize?: number = 10;
}

/**
 * 更新会话状态 DTO (内部使用)
 */
export class UpdateSessionStatusDto {
  @ApiProperty({ description: '会话状态', enum: WalletSessionStatus })
  @IsEnum(WalletSessionStatus)
  status: WalletSessionStatus;

  @ApiPropertyOptional({ description: '当前轮次' })
  @IsOptional()
  @IsNumber()
  currentRound?: number;

  @ApiPropertyOptional({ description: '公钥' })
  @IsOptional()
  @IsString()
  publicKey?: string;

  @ApiPropertyOptional({ description: '错误信息' })
  @IsOptional()
  @IsString()
  error?: string;
}
