import { z } from "zod";

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

export const PrivateRoomProfileSchema = z.object({
  characterId: stringOrEmpty,
  displayName: stringOrEmpty,
  authorName: stringOrEmpty,
  avatarUrl: stringOrNull,
  coverUrl: stringOrNull,
  title: stringOrEmpty,
  subtitle: stringOrEmpty,
});

export const PrivateRoomImageSchema = z.object({
  url: stringOrNull,
  type: stringOrEmpty,
  locked: booleanOrFalse,
  index: numberOrZero,
});

export const PrivateRoomLockDetailSchema = z.object({
  locked: booleanOrFalse,
  showContent: booleanOrFalse,
  showUpgrade: booleanOrFalse,
  reason: stringOrNull,
  hint: stringOrNull,
  actionLabel: stringOrNull,
  type: stringOrNull,
  requiredCredits: numberOrZero,
  currentCredits: numberOrZero,
  shortfallCredits: numberOrZero,
  mediaCount: numberOrZero,
  unlockCostPerImage: numberOrZero,
  detail: z.unknown().nullable().default(null),
});

export const PrivateRoomAuthorSchema = z.object({
  id: stringOrEmpty,
  name: stringOrEmpty,
  avatarUrl: stringOrNull,
});

export const PrivateRoomMomentSchema = z.object({
  momentId: stringOrEmpty,
  source: stringOrEmpty,
  sourceId: stringOrEmpty,
  characterId: stringOrEmpty,
  author: schemaOr(PrivateRoomAuthorSchema, {
    id: "",
    name: "",
    avatarUrl: null,
  }),
  createdAt: stringOrNull,
  publishedAt: stringOrNull,
  timeText: stringOrEmpty,
  title: stringOrEmpty,
  content: stringOrNull,
  text: stringOrNull,
  textPreview: stringOrEmpty,
  mediaCount: numberOrZero,
  images: arrayOrEmpty(PrivateRoomImageSchema),
  locked: booleanOrFalse,
  unlocked: booleanOrFalse,
  unlockCost: numberOrZero,
  unlockCostPerImage: numberOrZero,
  requiredCredits: numberOrZero,
  currency: stringOrEmpty,
  lockDetail: schemaOr(PrivateRoomLockDetailSchema, {
    locked: false,
    showContent: false,
    showUpgrade: false,
    reason: null,
    hint: null,
    actionLabel: null,
    type: null,
    requiredCredits: 0,
    currentCredits: 0,
    shortfallCredits: 0,
    mediaCount: 0,
    unlockCostPerImage: 0,
    detail: null,
  }),
});

export type PrivateRoomProfileData = z.output<
  typeof PrivateRoomProfileSchema
>;
export type PrivateRoomImageData = z.output<typeof PrivateRoomImageSchema>;
export type PrivateRoomLockDetailData = z.output<
  typeof PrivateRoomLockDetailSchema
>;
export type PrivateRoomAuthorData = z.output<typeof PrivateRoomAuthorSchema>;
export type PrivateRoomMomentInput = z.input<typeof PrivateRoomMomentSchema>;
export type PrivateRoomMomentData = z.output<typeof PrivateRoomMomentSchema>;
