import type { UserView } from "@/data/dto/user";
import type { UserEntitlementSnapshotData } from "@/data/schemas/user";

export function toView(u: {
  id: string;
  username: string;
  countryCode?: string;
  dolBalance?: number;
  creditBalance?: number;
  dailyFreeChatLimit?: number;
  dailyFreeChatRemaining?: number;
  dailyFreePrivateLimit?: number;
  dailyFreePrivateRemaining?: number;
  isVip?: boolean;
}): UserView {
  const creditBalance = u.creditBalance ?? u.dolBalance ?? 0;

  return {
    id: u.id,
    username: u.username,
    countryCode: u.countryCode ?? "",
    creditBalance,
    dailyFreeChatLimit: u.dailyFreeChatLimit ?? 0,
    dailyFreeChatRemaining: u.dailyFreeChatRemaining ?? 0,
    dailyFreePrivateLimit: u.dailyFreePrivateLimit ?? 0,
    dailyFreePrivateRemaining: u.dailyFreePrivateRemaining ?? 0,
    isVip: u.isVip ?? false,
  };
}

export function applyEntitlementSnapshotToView(
  user: UserView | null,
  snapshot: UserEntitlementSnapshotData | null,
): UserView | null {
  if (!user || !snapshot) return user;
  return {
    ...user,
    isVip: snapshot.isVip,
    creditBalance: snapshot.creditBalance,
  };
}

export function toEntitlementSnapshot(input: {
  isVip: boolean;
  creditBalance: number;
}): UserEntitlementSnapshotData {
  return {
    isVip: input.isVip,
    creditBalance: input.creditBalance,
  };
}
