import type { PaymentGateway } from "@/types/commerce";

export type SubscriptionStatus = "active" | "cancelled" | "past_due" | "trialing";
export type PaymentStatus = "pending" | "paid" | "failed" | "refunded";
export type InvoiceStatus = "draft" | "open" | "paid" | "void" | "uncollectible";

export interface BillingPlanSummary {
  id: number;
  name: string;
  slug: string;
  catalog?: string | null;
  limits?: { apps: number; users: number; support: string };
}

export interface BillingAddonSummary {
  id: number;
  name: string;
  slug: string;
  price: number;
}

export interface BillingSubscription {
  id: number;
  status: SubscriptionStatus;
  billing_cycle: "monthly" | "yearly";
  gateway: PaymentGateway | string;
  amount: number;
  next_payment: string | null;
  expires_at: string | null;
  starts_at: string | null;
  plan: BillingPlanSummary | null;
  addons: BillingAddonSummary[];
  pending_downgrade: {
    plan_id: number;
    plan_name: string | null;
    effective_at: string | null;
  } | null;
}

export interface BillingPayment {
  id: number;
  subscription_id: number | null;
  gateway: string;
  transaction_id: string | null;
  amount: number;
  currency: string;
  status: PaymentStatus;
  payment_method: string | null;
  paid_at: string | null;
  created_at: string | null;
}

export interface BillingInvoice {
  id: number;
  number: string;
  amount: number;
  currency: string;
  status: InvoiceStatus;
  due_at: string | null;
  paid_at: string | null;
  pdf_url: string | null;
  line_items: { label: string; amount: number }[] | null;
  created_at: string | null;
}

export interface BillingOverview {
  subscription: BillingSubscription | null;
  payments: BillingPayment[];
  invoices: BillingInvoice[];
  available_plans: BillingPlanSummary[];
  metrics: {
    days_until_renewal: number | null;
    is_past_due: boolean;
  };
}
