/**
 * Amounts are decimals in the currency's major unit (24.9 = 24.90 EUR). Pass a string ("24.90") for exact values;
 * the SDK sends all amounts as strings, which the API accepts.
 */
export type Amount = number | string;

export type PaymentStatus = "pending" | "completed" | "aborted";
export type SubscriptionStatus = "offered" | "active" | "canceled";
export type Interval = "day" | "week" | "month" | "year";

/** A payment (or a refund, `type: "refund"`), as `getPayment` returns it. */
export interface Payment {
  /** A random string like "tx_8fK2mQ7xRb4Tn9WcYz3LpH6v", not a number. */
  id: string;
  type: "payment" | "refund" | (string & {});
  amount: number;
  currency: string;
  status: PaymentStatus | (string & {});
  description: string;
  /** Set once the payment is completed. */
  reference: string | null;
  /** Set once a QR code was issued. */
  payment_code: string | null;
  refunded: number;
  refundable: number;
  refund_of: string | null;
  subscription_id: string | null;
  /** Every subscription this payment belongs to: the one it charged, or all those a checkout of several started. */
  subscription_ids: string[];
}

/** A QR code for a pending payment: put `url` in a QR code, or show the ready-made SVG at `qr_svg`. */
export interface PaymentCode {
  code: string;
  url: string;
  qr_svg: string;
}

export interface Refund {
  id: string;
  amount: number;
  currency: string;
  reference: string;
  reason: string | null;
}

export interface RefundResult {
  refund: Refund;
  /** The refunded payment, with updated `refunded` / `refundable`. */
  transaction: Payment;
}

/** Timestamps are ISO 8601 strings in UTC. */
export interface Subscription {
  /** A random string like "sub_8fK2mQ7xRb4Tn9WcYz3LpH6v", not a number. */
  id: string;
  status: SubscriptionStatus | (string & {});
  /** Within a period that's been paid for: give access while this is true. */
  active_period: boolean;
  amount: number;
  currency: string;
  interval: Interval | (string & {});
  interval_count: number;
  description: string | null;
  /** Send this to the customer. */
  url: string;
  qr_svg: string;
  subscribed_at: string | null;
  subscribed_for_days: number | null;
  subscribed_for_seconds: number | null;
  canceled: boolean;
  canceled_at: string | null;
  canceled_by: "customer" | "merchant" | "bank" | "schedule" | (string & {}) | null;
  current_period_start: string | null;
  current_period_end: string | null;
  next_charge_at: string | null;
  ends_at: string | null;
  last_day: string | null;
  charges: number;
  last_error: string | null;
  customer: { name: string } | null;
}

/** The first payment of a subscription: hand `transaction_id` to the pay button / hosted checkout. */
export interface SubscriptionCheckout {
  transaction_id: string;
  subscription: Subscription;
}

/** One first payment for several subscriptions: paying `transaction_id` starts all of `subscriptions` (in your order). */
export interface SubscriptionsCheckout {
  transaction_id: string;
  subscriptions: Subscription[];
}

export interface Rate {
  /** Converts `currency_from` into `currency_to`. */
  to_rate: number;
  /** The other way. */
  from_rate: number;
}

export interface CreatePaymentParams {
  amount: Amount;
  /** ISO code, e.g. "EUR". */
  currency: string;
  /** Shown to the customer when they pay, and in their history. */
  description?: string;
  /** Where HBank asks you to approve the payment. Required for the hosted checkout. */
  callback_url?: string;
}

export interface RefundParams {
  /** In the payment's currency. Left out: everything that's left. */
  amount?: Amount;
  /** Up to 200 characters, shown to the customer. */
  reason?: string;
}

export interface CreateSubscriptionParams {
  amount: Amount;
  currency: string;
  interval: Interval;
  /** Every how many intervals, 1 to 120. Default 1. */
  interval_count?: number;
  /** Up to 200 characters, shown to the customer. */
  description?: string;
}

export interface SubscriptionCheckoutParams extends CreateSubscriptionParams {
  callback_url?: string;
}

export interface SubscriptionsCheckoutParams {
  /** Up to 20, in one currency: the id of a subscription you offered ("sub_…") or a new offer's fields. */
  subscriptions: (string | CreateSubscriptionParams)[];
  /** What the payment is for. Default: the subscriptions' descriptions. */
  description?: string;
  callback_url?: string;
}
