export function apiEnvelope<T>(data: T) {
  return {
    code: 200,
    message: "success",
    success: true,
    data,
  };
}

export const e2eUser = {
  id: "user_e2e",
  username: "E2E User",
  email: "e2e@example.com",
  platform: "web",
  intimacy: 42,
  dolBalance: 0,
  relationshipStage: "close_friend",
  currentMood: "happy",
  preferredLanguage: "en",
  createdAt: "2026-06-25T00:00:00.000Z",
  lastMessageAt: "",
  avatarUrl: "",
  loginProvider: "guest",
  isGuest: true,
  isVip: false,
  voiceMinutesRemaining: 0,
};

export const e2eEmailUser = {
  ...e2eUser,
  id: "user_e2e_email",
  username: "E2E Email User",
  email: "chanwillian0@gmail.com",
  isGuest: false,
};

export const guestLoginResponse = {
  token: "e2e-guest-token",
  deviceId: "e2e-device-id",
  userId: e2eUser.id,
  isDeviceUser: true,
  expiresIn: 2_592_000,
  user: e2eUser,
};

export const refreshedGuestLoginResponse = {
  ...guestLoginResponse,
  token: "e2e-refreshed-guest-token",
};

export const emailLoginResponse = {
  token: "e2e-email-token",
  refreshToken: "e2e-email-refresh-token",
  user: e2eEmailUser,
};

export const refreshedEmailLoginResponse = {
  token: "e2e-refreshed-email-token",
  refreshToken: "e2e-refreshed-email-refresh-token",
};

export const userEntitlementsResponse = {
  userId: e2eEmailUser.id,
  isGuest: false,
  isVip: false,
  vipExpiresAt: null,
  creditBalance: 0,
  dolBalance: 0,
};

export const emptyChatHistoryResponse = {
  messages: [],
  total: 0,
  limit: 50,
  offset: 0,
  isVip: false,
  privateFreeLimit: 0,
  privateUsedToday: 0,
  privateCanViewFree: false,
};

export const chatSendResponse = {
  reply: "Hello from the E2E boyfriend.",
  audioUrl: "",
  messageId: "msg_e2e_reply_001",
  isGuest: true,
  timestamp: 1_782_356_425_363,
  image: {
    type: null,
    url: null,
  },
  lockDetail: {
    locked: false,
    showContent: true,
    showUpgrade: false,
    reason: null,
    hint: null,
    detail: null,
  },
};

export function createChatSendResponse(sendCount: number) {
  return {
    ...chatSendResponse,
    reply: `Hello from the E2E boyfriend. Reply ${sendCount}.`,
    messageId: `msg_e2e_reply_${String(sendCount).padStart(3, "0")}`,
    timestamp: chatSendResponse.timestamp + sendCount,
  };
}

export function createWeeklyLimitChatSendResponse(
  usedMessageCount: number,
  limit: number,
) {
  return {
    reply: "",
    audioUrl: "",
    messageId: "",
    isGuest: true,
    canSendMessage: false,
    cannotSendReason: "insufficient_credits",
    creditBalance: 0,
    creditsCharged: 0,
    requiredCredits: limit,
    shortfallCredits: Math.max(0, limit - usedMessageCount),
    timestamp: chatSendResponse.timestamp + usedMessageCount,
    image: {
      type: null,
      url: null,
    },
    lockDetail: {
      locked: true,
      showContent: false,
      showUpgrade: true,
      reason: "weekly_limit",
      hint: "Free message limit reached.",
      detail: {
        type: "weekly_msg_limit",
        usedThisWeek: usedMessageCount,
        limit,
      },
    },
  };
}

export const paidImageMessageId = "msg_photo_paywall_001";
export const paidImageUrl =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/p9sAAAAASUVORK5CYII=";

export const paidImageChatSendResponse = {
  reply: "I can show you that photo after you activate VIP.",
  audioUrl: "",
  messageId: paidImageMessageId,
  isGuest: true,
  timestamp: 1_782_180_725_000,
  image: {
    type: "elio_schedule",
    url: paidImageUrl,
  },
  lockDetail: {
    locked: true,
    showContent: true,
    showUpgrade: true,
    reason: "image",
    hint: "Activate VIP to unlock the full photo.",
    detail: null,
  },
};

export function createPaidImageHistoryResponse(unlocked: boolean) {
  return {
    messages: [
      {
        role: "assistant",
        type: "image",
        content: unlocked
          ? ""
          : "I can show you that photo after you activate VIP.",
        id: paidImageMessageId,
        created_at: "2026-06-30T00:00:00.000Z",
        audioUrl: null,
        image: {
          type: "elio_schedule",
          url: paidImageUrl,
        },
        lockDetail: unlocked
          ? {
              locked: false,
              showContent: true,
              showUpgrade: false,
              reason: null,
              hint: null,
              detail: null,
            }
          : paidImageChatSendResponse.lockDetail,
      },
    ],
    total: 1,
    limit: 50,
    offset: 0,
    isVip: unlocked,
    privateFreeLimit: 0,
    privateUsedToday: 0,
    privateCanViewFree: false,
  };
}

export const paidImageUnlockHistoryResponse = {
  unlocked: true,
  reason: "ok",
  totalLocked: 1,
  unlockedCount: 1,
  privateCount: 0,
  imageCount: 1,
  voiceCount: 0,
  requiredCredits: 0,
  currentCredits: 0,
  remainingCredits: 0,
  shortfallCredits: 0,
  costsByMessage: {
    [paidImageMessageId]: 0,
  },
  messageIds: [paidImageMessageId],
};

export const paidVoiceMessageId = "msg_voice_paywall_001";
export const paidVoiceAudioUrl =
  "data:audio/mpeg;base64,//uQZAAAAAAAAAAAAAAAAAAAAAAAWGluZwAAAA8AAAACAAACcQCA";

export const paidVoiceHistoryResponse = {
  messages: [
    {
      role: "assistant",
      type: "voice",
      content: "",
      id: paidVoiceMessageId,
      created_at: "2026-07-01T00:00:00.000Z",
      audioUrl: paidVoiceAudioUrl,
      image: {
        type: null,
        url: null,
      },
      lockDetail: {
        locked: true,
        showContent: true,
        showUpgrade: true,
        reason: "voice_message",
        hint: "Unlock this voice message with credits.",
        detail: null,
      },
    },
  ],
  total: 1,
  limit: 50,
  offset: 0,
  isVip: false,
  privateFreeLimit: 0,
  privateUsedToday: 0,
  privateCanViewFree: false,
};

export const insufficientCreditsUnlockVoiceResponse = {
  unlocked: false,
  content: "",
  reason: "insufficient_balance",
  creditBalance: 3,
  creditsCharged: 0,
  requiredCredits: 20,
  shortfallCredits: 17,
  lockDetail: {
    locked: true,
    showContent: true,
    showUpgrade: true,
    reason: "voice_message",
    hint: "Unlock this voice message with credits.",
    detail: null,
  },
};

export const insufficientCreditsUnlockImageResponse = {
  unlocked: false,
  content: "",
  reason: "insufficient_balance",
  creditBalance: 3,
  creditsCharged: 0,
  requiredCredits: 30,
  shortfallCredits: 27,
  lockDetail: {
    locked: true,
    showContent: true,
    showUpgrade: true,
    reason: "image",
    hint: "Unlock this high-definition image with credits.",
    detail: null,
  },
};

export const paymentPlansResponse = {
  plans: [
    {
      planId: "vip_monthly",
      planName: "Monthly",
      orderType: "vip_monthly",
      amountCents: 999,
      originalAmountCents: 1999,
      dailyPriceCents: 33,
      currency: "USD",
      vipDays: 30,
      dolAmount: null,
      creditBalance: 300,
    },
    {
      planId: "vip_quarterly",
      planName: "Quarterly",
      orderType: "vip_quarterly",
      amountCents: 2499,
      originalAmountCents: 5999,
      dailyPriceCents: 28,
      currency: "USD",
      vipDays: 90,
      dolAmount: null,
      creditBalance: 1000,
    },
    {
      planId: "dol_voice_30",
      planName: "Voice Pack",
      orderType: "dol_voice",
      amountCents: 499,
      originalAmountCents: null,
      dailyPriceCents: null,
      currency: "USD",
      vipDays: null,
      dolAmount: 30,
      creditBalance: 30,
    },
  ],
};

export const paymentOrderId = "order_e2e_vip_monthly";

export const createPaymentOrderResponse = {
  orderId: paymentOrderId,
  payParams: {
    provider: "stripe",
    clientSecret: "pi_e2e_secret_mock",
  },
};

export const paidPaymentOrderStatusResponse = {
  orderId: paymentOrderId,
  status: "paid",
  orderType: "vip_monthly",
  planId: "vip_monthly",
};

export const vipStatusResponse = {
  isVip: false,
  expiresAt: null,
};
