import { z } from "zod";

import { ERetainingFunnel } from "@/stores/retaining-store";
import { Currency } from "@/types";

export const SubscriptionTypeEnum = z.enum(["DAY", "WEEK", "MONTH", "YEAR"]);
export type SubscriptionType = z.infer<typeof SubscriptionTypeEnum>;

export const SubscriptionStatusEnum = z.enum([
  "ACTIVE",
  "CANCELLED",
  "PAST_DUE",
]);
export type SubscriptionStatus = z.infer<typeof SubscriptionStatusEnum>;

export const UserSubscriptionSchema = z.object({
  id: z.string(),
  subscriptionType: SubscriptionTypeEnum,
  subscriptionStatus: SubscriptionStatusEnum,
  billingPeriod: SubscriptionTypeEnum,
  lastPaymentOn: z.string().nullable(),
  renewalDate: z.string().nullable(),
  renewalAmount: z.number(),
  currency: z.nativeEnum(Currency),
  cancellationDate: z.string().nullable(),
});
export type UserSubscription = z.infer<typeof UserSubscriptionSchema>;

export const SubscriptionsSchema = z.object({
  status: z.string(),
  data: z.array(UserSubscriptionSchema),
});
export type SubscriptionsData = z.infer<typeof SubscriptionsSchema>;

export const UserSubscriptionActionEnumSchema = z.enum([
  "cancel",
  "discount_50",
  "pause_30",
  "pause_60",
  "free_chat_30",
]);
export type UserSubscriptionActionEnum = z.infer<
  typeof UserSubscriptionActionEnumSchema
>;

export const UserSubscriptionActionPayloadSchema = z.object({
  subscriptionId: z.string(),
  action: UserSubscriptionActionEnumSchema,
  retainingJourney: z
    .object({
      startedAt: z.number(),
      updatedAt: z.number(),
      currentStep: z.string(),
      choices: z.array(
        z.object({
          step: z.string(),
          choiceId: z.union([z.string(), z.number()]),
          choiceTitle: z.string(),
          timestamp: z.number(),
        })
      ),
      funnel: z.nativeEnum(ERetainingFunnel),
      completedSteps: z.array(z.string()),
    })
    .optional(), // Include retaining journey data when action comes from funnel
});
export type UserSubscriptionActionPayload = z.infer<
  typeof UserSubscriptionActionPayloadSchema
>;

export const UserSubscriptionActionResponseSchema = z.object({
  status: z.string(),
  data: UserSubscriptionSchema,
});
export type UserSubscriptionActionResponse = z.infer<
  typeof UserSubscriptionActionResponseSchema
>;
