/**
 * Subscription and payment types — ported from user-dashboard-web repos.
 */

import { z } from 'zod';

export interface SubscriptionDoc {
  id: string;
  productId: string;
  userId: string;
  plan: 'free' | 'pro' | 'enterprise';
  status: 'active' | 'cancelled' | 'past_due' | 'trialing';
  currentPeriodStart: string;
  currentPeriodEnd: string;
  cancelAtPeriodEnd: boolean;
  monthlyPrice: number;
  tokensIncluded: number;
  tokensUsed: number;
  stripeCustomerId?: string;
  stripeSubscriptionId?: string;
  createdAt: string;
  updatedAt: string;
}

export interface PaymentDoc {
  id: string;
  productId: string;
  userId: string;
  amount: number; // Amount in smallest currency unit (cents for USD)
  currency: string;
  status: 'succeeded' | 'pending' | 'failed' | 'refunded';
  description: string;
  method: string;
  invoiceUrl?: string;
  createdAt: string;
}

export const CreateSubscriptionSchema = z.object({
  userId: z.string().min(1),
  plan: z.enum(['free', 'pro', 'enterprise']),
  status: z.enum(['active', 'cancelled', 'past_due', 'trialing']).default('active'),
  monthlyPrice: z.number().min(0),
  tokensIncluded: z.number().int().min(0),
  stripeCustomerId: z.string().optional(),
  stripeSubscriptionId: z.string().optional(),
  trialDays: z.number().int().min(0).optional(),
});

export const UpdateSubscriptionSchema = z.object({
  plan: z.enum(['free', 'pro', 'enterprise']).optional(),
  status: z.enum(['active', 'cancelled', 'past_due', 'trialing']).optional(),
  monthlyPrice: z.number().min(0).optional(),
  tokensIncluded: z.number().int().min(0).optional(),
  cancelAtPeriodEnd: z.boolean().optional(),
  stripeCustomerId: z.string().optional(),
  stripeSubscriptionId: z.string().optional(),
});

export const CreatePaymentSchema = z.object({
  userId: z.string().min(1),
  amount: z.number().int().min(0),
  currency: z.string().default('usd'),
  status: z.enum(['succeeded', 'pending', 'failed', 'refunded']),
  description: z.string().default(''),
  method: z.string().default('card'),
  invoiceUrl: z.string().optional(),
});

export type CreateSubscriptionInput = z.infer<typeof CreateSubscriptionSchema>;
export type UpdateSubscriptionInput = z.infer<typeof UpdateSubscriptionSchema>;
export type CreatePaymentInput = z.infer<typeof CreatePaymentSchema>;
