/**
 * 用户权益快照
 */
import { z } from "zod";

import {
  booleanOrFalse,
  numberOrZero,
  schemaOr,
  stringOrEmpty,
  stringOrNull,
} from "../nullable-defaults";

const POLICY_DEFAULTS = {
  membershipState: "",
  nonVipEntitlementsShared: false,
  guestSameAsLoggedInNonVip: false,
  refreshAfterPayment: "",
} as const;

const COSTS_DEFAULTS = {
  normal_message: 0,
  private_message: 0,
  voice_message: 0,
  photo: 0,
  voice_call_minute: 0,
  private_album_10: 0,
  private_album_20: 0,
} as const;

const QUOTAS_DEFAULTS = {
  normalChatFreeDaily: 0,
  privateUnlockFreeDaily: 0,
  voiceMessageFreeDaily: 0,
  photoFreeDaily: 0,
} as const;

const HISTORY_UNLOCK_COSTS_DEFAULTS = {
  private_message: 0,
  voice_message: 0,
  photo: 0,
} as const;

const HISTORY_UNLOCK_DEFAULTS = {
  enabled: false,
  order: "",
  chargeMode: "",
  insufficientBalanceBehavior: "",
  costs: HISTORY_UNLOCK_COSTS_DEFAULTS,
} as const;

export const UserEntitlementsPolicySchema = z
  .object({
    membershipState: stringOrEmpty,
    nonVipEntitlementsShared: booleanOrFalse,
    guestSameAsLoggedInNonVip: booleanOrFalse,
    refreshAfterPayment: stringOrEmpty,
  })
  .passthrough();

export const UserEntitlementsCostsSchema = z
  .object({
    normal_message: numberOrZero,
    private_message: numberOrZero,
    voice_message: numberOrZero,
    photo: numberOrZero,
    voice_call_minute: numberOrZero,
    private_album_10: numberOrZero,
    private_album_20: numberOrZero,
  })
  .passthrough();

export const UserEntitlementsQuotasSchema = z
  .object({
    normalChatFreeDaily: numberOrZero,
    privateUnlockFreeDaily: numberOrZero,
    voiceMessageFreeDaily: numberOrZero,
    photoFreeDaily: numberOrZero,
  })
  .passthrough();

export const UserEntitlementsHistoryUnlockCostsSchema = z
  .object({
    private_message: numberOrZero,
    voice_message: numberOrZero,
    photo: numberOrZero,
  })
  .passthrough();

export const UserEntitlementsHistoryUnlockSchema = z
  .object({
    enabled: booleanOrFalse,
    order: stringOrEmpty,
    chargeMode: stringOrEmpty,
    insufficientBalanceBehavior: stringOrEmpty,
    costs: schemaOr(
      UserEntitlementsHistoryUnlockCostsSchema,
      HISTORY_UNLOCK_COSTS_DEFAULTS,
    ),
  })
  .passthrough();

export const UserEntitlementsSchema = z.object({
  userId: stringOrEmpty,
  isGuest: booleanOrFalse,
  isVip: booleanOrFalse,
  vipExpiresAt: stringOrNull,
  creditBalance: numberOrZero,
  dolBalance: numberOrZero,
  policy: schemaOr(UserEntitlementsPolicySchema, POLICY_DEFAULTS),
  costs: schemaOr(UserEntitlementsCostsSchema, COSTS_DEFAULTS),
  quotas: schemaOr(UserEntitlementsQuotasSchema, QUOTAS_DEFAULTS),
  historyUnlock: schemaOr(
    UserEntitlementsHistoryUnlockSchema,
    HISTORY_UNLOCK_DEFAULTS,
  ),
});

export type UserEntitlementsInput = z.input<typeof UserEntitlementsSchema>;
export type UserEntitlementsData = z.output<typeof UserEntitlementsSchema>;
