import type { CatalogVariant } from "./engine/costing";
import type { LotRecord, ShopSettings } from "./engine/store-types";
import type { Errors } from "./engine/validation";

// What PHP hands over before the page starts (Admin::boot_data in includes/class-admin.php).
export interface Boot {
  version: string;
  currency: string;
  locale: string;
  restNamespace: string;
}

declare global {
  interface Window {
    lotcostBoot?: Boot;
  }
}

export interface CogsStatus {
  supported: boolean;
  enabled: boolean;
  settingsUrl: string;
}

// A piece (a simple product or one variation) as WooCommerce describes it today (Catalog::variant_info).
// `productTitle` is the product's name (the parent's for a variation); `title` is the variation label, "" for a simple product.
export interface VariantInfo extends CatalogVariant {
  productId: string;
  productTitle: string;
}

export interface VariantHit extends VariantInfo {
  // The name of the other lot that holds this piece, or null when it is free.
  inLot: string | null;
}

export interface ProductHit {
  id: string;
  title: string;
  type: "simple" | "variable";
  variants: VariantHit[];
}

export interface StateResponse {
  settings: ShopSettings;
  lots: LotRecord[];
  catalog: Record<string, VariantInfo>;
  cogs: CogsStatus;
}

export interface WriteResult {
  productId?: string;
  variantId: string;
  ok: boolean;
  previousCents?: number | null;
  newCents?: number;
  code?: string;
  message?: string;
}

export type SaveOutcome<T = void> = { ok: true; value: T } | { ok: false; errors: Errors };
