/**
 * Subscriptions Domain - Schemas
 * 
 * Zod validation schemas for subscription domain types.
 */

import { z } from "zod";

// Subscription Status Schema
export const subscriptionStatusSchema = z.enum([
  "Active",
  "Inactive",
  "Pending",
  "Cancelled",
  "Suspended",
  "Terminated",
  "Completed",
]);

// Subscription Cycle Schema
export const subscriptionCycleSchema = z.enum([
  "Monthly",
  "Quarterly",
  "Semi-Annually",
  "Annually",
  "Biennially",
  "Triennially",
  "One-time",
  "Free",
]);

// Subscription Schema
export const subscriptionSchema = z.object({
  id: z.number().int().positive("Subscription id must be positive"),
  serviceId: z.number().int().positive("Service id must be positive"),
  productName: z.string().min(1, "Product name is required"),
  domain: z.string().optional(),
  cycle: subscriptionCycleSchema,
  status: subscriptionStatusSchema,
  nextDue: z.string().optional(),
  amount: z.number(),
  currency: z.string().min(1, "Currency is required"),
  currencySymbol: z.string().optional(),
  registrationDate: z.string().min(1, "Registration date is required"),
  notes: z.string().optional(),
  customFields: z.record(z.string(), z.string()).optional(),
  orderNumber: z.string().optional(),
  groupName: z.string().optional(),
  paymentMethod: z.string().optional(),
  serverName: z.string().optional(),
});

// Subscription List Schema
export const subscriptionListSchema = z.object({
  subscriptions: z.array(subscriptionSchema),
  totalCount: z.number().int().nonnegative(),
});

// ============================================================================
// Query Parameter Schemas
// ============================================================================

/**
 * Schema for subscription query parameters
 */
export const subscriptionQueryParamsSchema = z.object({
  page: z.coerce.number().int().positive().optional(),
  limit: z.coerce.number().int().positive().max(100).optional(),
  status: subscriptionStatusSchema.optional(),
  type: z.string().optional(),
});

export type SubscriptionQueryParams = z.infer<typeof subscriptionQueryParamsSchema>;

export const subscriptionQuerySchema = subscriptionQueryParamsSchema;
export type SubscriptionQuery = SubscriptionQueryParams;

// ============================================================================
// Response Schemas
// ============================================================================

/**
 * Schema for subscription statistics
 */
export const subscriptionStatsSchema = z.object({
  total: z.number().int().nonnegative(),
  active: z.number().int().nonnegative(),
  completed: z.number().int().nonnegative(),
  cancelled: z.number().int().nonnegative(),
});

/**
 * Schema for SIM action responses (top-up, cancellation, feature updates)
 */
export const simActionResponseSchema = z.object({
  success: z.boolean(),
  message: z.string(),
  data: z.unknown().optional(),
});

/**
 * Schema for SIM plan change result with IP addresses
 */
export const simPlanChangeResultSchema = z.object({
  success: z.boolean(),
  message: z.string(),
  ipv4: z.string().optional(),
  ipv6: z.string().optional(),
});

// ============================================================================
// Inferred Types from Schemas (Schema-First Approach)
// ============================================================================

export type SubscriptionStatus = z.infer<typeof subscriptionStatusSchema>;
export type SubscriptionCycle = z.infer<typeof subscriptionCycleSchema>;
export type Subscription = z.infer<typeof subscriptionSchema>;
export type SubscriptionList = z.infer<typeof subscriptionListSchema>;
export type SubscriptionStats = z.infer<typeof subscriptionStatsSchema>;
export type SimActionResponse = z.infer<typeof simActionResponseSchema>;
export type SimPlanChangeResult = z.infer<typeof simPlanChangeResultSchema>;
