/**
 * @ai-summary Type definitions for subscriptions feature
 * @ai-context Core business types for Stripe subscription management
 */

// Subscription tier types (matches DB enum)
export type SubscriptionTier = 'free' | 'pro' | 'enterprise';

// Subscription status types (matches DB enum)
export type SubscriptionStatus = 'active' | 'past_due' | 'canceled' | 'unpaid';

// Billing cycle types (matches DB enum)
export type BillingCycle = 'monthly' | 'yearly';

// Donation status types (matches DB enum)
export type DonationStatus = 'pending' | 'succeeded' | 'failed' | 'canceled';

// Main subscription entity
export interface Subscription {
  id: string;
  userId: string;
  stripeCustomerId: string;
  stripeSubscriptionId?: string;
  tier: SubscriptionTier;
  billingCycle?: BillingCycle;
  status: SubscriptionStatus;
  currentPeriodStart?: Date;
  currentPeriodEnd?: Date;
  gracePeriodEnd?: Date;
  cancelAtPeriodEnd: boolean;
  createdAt: Date;
  updatedAt: Date;
}

// Subscription event entity (webhook event logging)
export interface SubscriptionEvent {
  id: string;
  subscriptionId: string;
  stripeEventId: string;
  eventType: string;
  payload: Record<string, any>;
  createdAt: Date;
}

// Donation entity (one-time payments)
export interface Donation {
  id: string;
  userId: string;
  stripePaymentIntentId: string;
  amountCents: number;
  currency: string;
  status: DonationStatus;
  createdAt: Date;
  updatedAt: Date;
}

// Tier vehicle selection entity (tracks which vehicles user selected to keep during downgrade)
export interface TierVehicleSelection {
  id: string;
  userId: string;
  vehicleId: string;
  selectedAt: Date;
}

// Request/Response types

export interface CreateSubscriptionRequest {
  userId: string;
  tier: SubscriptionTier;
  billingCycle: BillingCycle;
  paymentMethodId?: string;
}

export interface SubscriptionResponse {
  id: string;
  userId: string;
  stripeCustomerId: string;
  stripeSubscriptionId?: string;
  tier: SubscriptionTier;
  billingCycle?: BillingCycle;
  status: SubscriptionStatus;
  currentPeriodStart?: string;
  currentPeriodEnd?: string;
  gracePeriodEnd?: string;
  cancelAtPeriodEnd: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface DonationResponse {
  id: string;
  userId: string;
  stripePaymentIntentId: string;
  amountCents: number;
  currency: string;
  status: DonationStatus;
  createdAt: string;
  updatedAt: string;
}

export interface CreateDonationRequest {
  userId: string;
  amountCents: number;
  currency?: string;
}

export interface CreateSubscriptionEventRequest {
  subscriptionId: string;
  stripeEventId: string;
  eventType: string;
  payload: Record<string, any>;
}

export interface CreateTierVehicleSelectionRequest {
  userId: string;
  vehicleId: string;
}

// Service layer types
export interface UpdateSubscriptionData {
  stripeSubscriptionId?: string;
  tier?: SubscriptionTier;
  billingCycle?: BillingCycle;
  status?: SubscriptionStatus;
  currentPeriodStart?: Date;
  currentPeriodEnd?: Date;
  gracePeriodEnd?: Date;
  cancelAtPeriodEnd?: boolean;
}

export interface UpdateDonationData {
  status?: DonationStatus;
}

// Needs vehicle selection check response
export interface NeedsVehicleSelectionResponse {
  needsSelection: boolean;
  vehicleCount: number;
  maxAllowed: number;
}
