import { IsString, IsOptional, Length, Matches, IsInt, Min, Max } from 'class-validator';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

export class ValidateReferralCodeDto {
  @ApiProperty({ description: '推荐码', example: 'RWA123ABC' })
  @IsString()
  @Length(6, 20)
  @Matches(/^[A-Z0-9]+$/, { message: '推荐码只能包含大写字母和数字' })
  code: string;
}

export class CreateReferralDto {
  @ApiProperty({ description: '用户ID', example: '123456789' })
  @IsString()
  userId: string;

  @ApiProperty({ description: '账户序列号 (新格式: D + YYMMDD + 5位序号)', example: 'D2512110008' })
  @IsString()
  accountSequence: string; // 格式: D + YYMMDD + 5位序号

  @ApiProperty({ description: '用户的推荐码（由 identity-service 生成）', example: 'ABC123' })
  @IsString()
  @Length(6, 20)
  referralCode: string;

  @ApiPropertyOptional({ description: '推荐人的推荐码', example: 'RWA123ABC' })
  @IsOptional()
  @IsString()
  @Length(6, 20)
  referrerCode?: string;

  @ApiPropertyOptional({ description: '邀请人账户序列号 (新格式: D + YYMMDD + 5位序号)', example: 'D2512110007' })
  @IsOptional()
  @IsString()
  inviterAccountSequence?: string;
}

export class GetDirectReferralsDto {
  @ApiPropertyOptional({ description: '每页数量', default: 50 })
  @IsOptional()
  @IsInt()
  @Min(1)
  @Max(100)
  limit?: number = 50;

  @ApiPropertyOptional({ description: '偏移量', default: 0 })
  @IsOptional()
  @IsInt()
  @Min(0)
  offset?: number = 0;
}

export class ReferralInfoResponseDto {
  @ApiProperty({ description: '用户ID' })
  userId: string;

  @ApiProperty({ description: '推荐码' })
  referralCode: string;

  @ApiProperty({ description: '推荐人ID', nullable: true })
  referrerId: string | null;

  @ApiProperty({ description: '推荐链深度' })
  referralChainDepth: number;

  @ApiProperty({ description: '直推人数' })
  directReferralCount: number;

  @ApiProperty({ description: '团队总认种量' })
  totalTeamCount: number;

  @ApiProperty({ description: '个人认种量' })
  personalPlantingCount: number;

  @ApiProperty({ description: '团队认种量' })
  teamPlantingCount: number;

  @ApiProperty({ description: '龙虎榜分值' })
  leaderboardScore: number;

  @ApiProperty({ description: '龙虎榜排名', nullable: true })
  leaderboardRank: number | null;

  @ApiProperty({ description: '创建时间' })
  createdAt: Date;
}

export class DirectReferralResponseDto {
  @ApiProperty({ description: '用户ID' })
  userId: string;

  @ApiProperty({ description: '账户序列号 (新格式: D + YYMMDD + 5位序号)' })
  accountSequence: string; // 格式: D + YYMMDD + 5位序号

  @ApiProperty({ description: '推荐码' })
  referralCode: string;

  @ApiProperty({ description: '个人认种量' })
  personalPlantingCount: number;

  @ApiProperty({ description: '团队认种量' })
  teamPlantingCount: number;

  @ApiProperty({ description: '直推人数（用于判断是否有下级）' })
  directReferralCount: number;

  @ApiProperty({ description: '加入时间' })
  joinedAt: Date;
}

export class DirectReferralsResponseDto {
  @ApiProperty({ type: [DirectReferralResponseDto] })
  referrals: DirectReferralResponseDto[];

  @ApiProperty({ description: '总数' })
  total: number;

  @ApiProperty({ description: '是否有更多' })
  hasMore: boolean;
}

export class ValidateCodeResponseDto {
  @ApiProperty({ description: '是否有效' })
  valid: boolean;

  @ApiPropertyOptional({ description: '推荐人ID' })
  referrerId?: string;
}
