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

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

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 UserEntitlementsHistoryUnlockSchema = z
  .object({
    enabled: booleanOrFalse,
    order: stringOrEmpty,
    chargeMode: stringOrEmpty,
    insufficientBalanceBehavior: stringOrEmpty,
    costs: z
      .object({
        private_message: numberOrZero,
        voice_message: numberOrZero,
        photo: numberOrZero,
      })
      .passthrough()
      .default(() => ({
        private_message: 0,
        voice_message: 0,
        photo: 0,
      })),
  })
  .passthrough();

export const UserEntitlementsSchema = z.object({
  userId: stringOrEmpty,
  isGuest: booleanOrFalse,
  isVip: booleanOrFalse,
  vipExpiresAt: z.string().nullable().default(null),
  creditBalance: numberOrZero,
  dolBalance: numberOrZero,
  policy: UserEntitlementsPolicySchema.default(() => ({
    membershipState: "",
    nonVipEntitlementsShared: false,
    guestSameAsLoggedInNonVip: false,
    refreshAfterPayment: "",
  })),
  costs: UserEntitlementsCostsSchema.default(() => ({
    normal_message: 0,
    private_message: 0,
    voice_message: 0,
    photo: 0,
    voice_call_minute: 0,
    private_album_10: 0,
    private_album_20: 0,
  })),
  quotas: UserEntitlementsQuotasSchema.default(() => ({
    normalChatFreeDaily: 0,
    privateUnlockFreeDaily: 0,
    voiceMessageFreeDaily: 0,
    photoFreeDaily: 0,
  })),
  historyUnlock: UserEntitlementsHistoryUnlockSchema.default(() => ({
    enabled: false,
    order: "",
    chargeMode: "",
    insufficientBalanceBehavior: "",
    costs: {
      private_message: 0,
      voice_message: 0,
      photo: 0,
    },
  })),
});

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