// Generated by bynkc — do not edit by hand.
// context commerce.payment

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "../runtime.js";

import { Money as __CommonsMoney } from "./money.js";

export type Money = __CommonsMoney & { readonly __ctxBrand: "commerce.payment" };

/**
 * Payment context. Authorises monetary transactions.
 */

export type AuthId = string & { readonly __brand: "commerce.payment.AuthId" };

export const AuthId = {
  of(value: string): Result<AuthId, ValidationError> {
    if (!new RegExp("^" + "AUTH-[0-9]{8}" + "$").test(value)) {
      return Err({ field: "AuthId", message: "must match /AUTH-[0-9]{8}/", value });
    }
    return Ok(value as AuthId);
  },
  unsafe(value: string): AuthId {
    return value as AuthId;
  },
};

export type PaymentError =
    { readonly tag: "Declined" }
  | { readonly tag: "InsufficientFunds" }
  | { readonly tag: "GatewayDown" };

export const PaymentError = {
  Declined: { tag: "Declined" } as PaymentError,
  InsufficientFunds: { tag: "InsufficientFunds" } as PaymentError,
  GatewayDown: { tag: "GatewayDown" } as PaymentError,
};

export interface Payments {
  authorise(amount: Money): Promise<Result<AuthId, PaymentError>>;
}

export const PaymentsToken: unique symbol = Symbol("Payments");

export class StubPayments implements Payments {
  async authorise(amount: Money): Promise<Result<AuthId, PaymentError>> {
    return (amount.minorUnits > 1000000 ? Err(PaymentError.Declined) : Ok(AuthId.unsafe("AUTH-12345678")));
  }
}

export const StubPaymentsProvider = { token: PaymentsToken, factory: () => new StubPayments() };

/**
 * Authorise a payment. The single entry point for payment authorisation
 * from other contexts.
 */
export const authorise = {
  async call(amount: Money, deps: { Payments: Payments }): Promise<Result<AuthId, PaymentError>> {
    const result = await deps.Payments.authorise(amount);
    return result;
  },
};

export interface CommercePaymentDeps {
  readonly Payments: Payments;
}

export function makeSurface(deps: CommercePaymentDeps) {
  return {
    async authorise(amount: Money): Promise<Result<AuthId, PaymentError>> {
      return authorise.call(amount, deps);
    },
  };
}

