import { z } from "zod";

import { Currency, ELocalesPlacement } from "../../../types";

// Request schemas
export const FunnelRequestSchema = z.object({
  funnel: z.nativeEnum(ELocalesPlacement),
});

// Response schemas
export const FunnelPaymentPropertySchema = z.object({
  key: z.string(),
  value: z.union([z.string(), z.number()]),
});

export const FunnelPaymentVariantSchema = z.object({
  id: z.string(),
  key: z.string(),
  type: z.string(),
  name: z.string().optional(),
  description: z.string().optional(),
  emoji: z.string().optional(),
  price: z.number(),
  oldPrice: z.number().optional(),
  trialPrice: z.number().optional(),
});

export const FunnelPaymentPlacementSchema = z.object({
  price: z.number().optional(),
  currency: z.nativeEnum(Currency).optional(),
  billingPeriod: z.enum(["DAY", "WEEK", "MONTH", "YEAR"]).optional(),
  billingInterval: z.number().optional(),
  trialPeriod: z.enum(["DAY", "WEEK", "MONTH", "YEAR"]).optional(),
  trialInterval: z.number().optional(),
  placementId: z.string().optional(),
  paywallId: z.string().optional(),
  properties: z.array(FunnelPaymentPropertySchema).optional(),
  variants: z.array(FunnelPaymentVariantSchema).optional(),
  paymentUrl: z.string().optional(),
  type: z.string().optional(),
  title: z.string().optional(),
});

export const FunnelSchema = z.object({
  currency: z.nativeEnum(Currency),
  funnel: z.nativeEnum(ELocalesPlacement),
  locale: z.string(),
  payment: z.record(
    z.string(),
    z.union([
      FunnelPaymentPlacementSchema.nullable(),
      z.array(FunnelPaymentPlacementSchema),
    ])
  ),
});

export const FunnelResponseSchema = z.object({
  status: z.union([z.literal("success"), z.string()]),
  data: FunnelSchema,
});

// Type exports
export type FunnelRequest = z.infer<typeof FunnelRequestSchema>;
export type IFunnelPaymentProperty = z.infer<
  typeof FunnelPaymentPropertySchema
>;
export type IFunnelPaymentVariant = z.infer<typeof FunnelPaymentVariantSchema>;
export type IFunnelPaymentPlacement = z.infer<
  typeof FunnelPaymentPlacementSchema
>;
export type IFunnel = z.infer<typeof FunnelSchema>;
export type FunnelResponse = z.infer<typeof FunnelResponseSchema>;
