/**
 * 用户统计响应 DTO
 */
import {
  UserStatsResponseSchema,
  type UserStatsResponseInput,
  type UserStatsResponseData,
} from "@/data/schemas/user/user_stats_response";
import { PersonalityTraits } from "./personality_traits";
import { RecentMemory } from "./recent_memory";

export class UserStatsResponse {
  declare readonly userId: string;
  declare readonly username: string;
  declare readonly platform: string;
  declare readonly intimacy: number;
  declare readonly relationshipStage: string;
  declare readonly dolBalance: number;
  declare readonly totalMessages: number;
  declare readonly lastMessageAt: string;
  declare readonly accountCreatedAt: string;
  declare readonly currentMood: string;
  declare readonly personalityTraits: PersonalityTraits;
  declare readonly recentMemories: RecentMemory[];
  declare readonly promptTokensTotal: number;
  declare readonly completionTokensTotal: number;
  declare readonly cacheHitTokensTotal: number;
  declare readonly tokensTotal: number;

  private constructor(input: UserStatsResponseInput) {
    const data = UserStatsResponseSchema.parse(input);
    Object.assign(this, {
      ...data,
      personalityTraits: PersonalityTraits.from(data.personalityTraits),
      recentMemories: data.recentMemories.map((m) => RecentMemory.from(m)),
    });
    Object.freeze(this);
  }

  static from(input: UserStatsResponseInput): UserStatsResponse {
    return new UserStatsResponse(input);
  }

  static fromJson(json: unknown): UserStatsResponse {
    return UserStatsResponse.from(json as UserStatsResponseInput);
  }

  toJson(): UserStatsResponseData {
    return UserStatsResponseSchema.parse(this);
  }
}
