import { z } from "zod";

import {
  booleanOrFalse,
  numberOrZero,
  stringOrEmpty,
  stringOrNull,
} from "../../nullable-defaults";
import { ChatImageSchema } from "../chat_payloads";
import { ChatLockTypeSchema } from "../chat_lock_type";

/**
 * 单条历史付费 / 私密消息解锁响应。
 */
export const UnlockPrivateReasonSchema = stringOrEmpty;

export const UnlockPrivateResponseSchema = z
  .object({
    unlocked: booleanOrFalse,
    content: stringOrEmpty,
    reply: stringOrEmpty,
    messageId: stringOrEmpty,
    message_id: stringOrEmpty,
    clientLockId: stringOrNull,
    lockType: ChatLockTypeSchema.nullable().default(null),
    audioUrl: stringOrEmpty,
    audio_url: stringOrEmpty,
    image: ChatImageSchema,
    reason: UnlockPrivateReasonSchema,
    creditBalance: numberOrZero,
    creditsCharged: numberOrZero,
    requiredCredits: numberOrZero,
    shortfallCredits: numberOrZero,
  })
  .transform(({ reply, message_id, audio_url, ...data }) => ({
    ...data,
    content: data.content || reply,
    messageId: data.messageId || message_id,
    audioUrl: data.audioUrl || audio_url,
  }));

export type UnlockPrivateReason = z.output<typeof UnlockPrivateReasonSchema>;
export type UnlockPrivateResponseInput = z.input<
  typeof UnlockPrivateResponseSchema
>;
export type UnlockPrivateResponseData = z.output<
  typeof UnlockPrivateResponseSchema
>;
