import type { CatalogProduct } from "./engine/costing";
import type { MaterialRecord, RecipeRecord, 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;
  proActive: boolean;
}

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

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

// A product as WooCommerce describes it (Catalog::describe).
export interface ProductInfo extends CatalogProduct {
  type: "simple" | "variable";
  status: string;
}

export interface ProductHit {
  id: string;
  title: string;
  type: "simple" | "variable";
  priceCents: number | null;
  hasRecipe: boolean;
}

export interface StateResponse {
  settings: ShopSettings;
  materials: MaterialRecord[];
  recipes: RecipeRecord[];
  products: Record<string, ProductInfo>;
  cogs: CogsStatus;
  // Product id -> (variant id -> coefficient); filled in by an add-on, empty otherwise.
  factors?: Record<string, Record<string, number>>;
}

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 };
