import { z } from "zod";

import {
  arrayOrEmpty,
  booleanOrFalse,
  booleanOrTrue,
  numberOrNull,
  numberOrZero,
  schemaOrNull,
  stringOrEmpty,
  stringOrNull,
} from "../nullable-defaults";
import { PaymentGuidanceSchema } from "../chat";

export const PrivateZoneVideoPostSchema = z
  .object({
    postId: stringOrEmpty,
    characterId: stringOrEmpty,
    caption: stringOrEmpty,
    posterUrl: stringOrNull,
    mediaType: z.literal("video").default("video"),
    videoUrl: stringOrNull,
    videoMimeType: stringOrEmpty,
    videoSizeBytes: numberOrZero,
    durationSeconds: numberOrNull,
    unlockCostCredits: numberOrZero,
    currency: z.literal("credits").default("credits"),
    locked: booleanOrFalse,
    unlocked: booleanOrFalse,
    availableForNewUnlock: booleanOrTrue,
    publishedAt: stringOrEmpty,
  })
  .readonly();

export const PrivateZonePostsResponseSchema = z
  .object({
    characterId: stringOrEmpty,
    items: arrayOrEmpty(PrivateZoneVideoPostSchema),
    nextCursor: stringOrNull,
    hasMore: booleanOrFalse,
    creditBalance: numberOrZero,
    currency: z.literal("credits").default("credits"),
  })
  .readonly();

export const PrivateZonePostUnlockReasonSchema = z.enum([
  "ok",
  "already_unlocked",
  "insufficient_credits",
  "cost_changed",
  "unavailable",
  "deduct_failed",
  "not_found",
]);

export const PrivateZonePostUnlockResponseSchema = z
  .object({
    postId: stringOrEmpty,
    reason: PrivateZonePostUnlockReasonSchema.or(stringOrEmpty),
    unlocked: booleanOrFalse,
    creditsCharged: numberOrZero,
    requiredCredits: numberOrZero,
    currentCredits: numberOrZero,
    shortfallCredits: numberOrZero,
    creditBalance: numberOrZero,
    post: schemaOrNull(PrivateZoneVideoPostSchema),
    paymentGuidance: PaymentGuidanceSchema.nullish(),
  })
  .readonly();

export const UnlockPrivateZonePostRequestSchema = z
  .object({
    expectedCost: z.number().int().min(1),
  })
  .readonly();

export type PrivateZoneVideoPost = z.output<typeof PrivateZoneVideoPostSchema>;
export type PrivateZonePostsResponse = z.output<typeof PrivateZonePostsResponseSchema>;
export type PrivateZonePostUnlockResponse = z.output<
  typeof PrivateZonePostUnlockResponseSchema
>;
export type UnlockPrivateZonePostRequest = z.output<
  typeof UnlockPrivateZonePostRequestSchema
>;
