// API + config types for the WooCommerce widget.
//
// Adapted from the Shopify widget. The big difference: quota/ledger enforcement
// is handled SERVER-SIDE by the brain storefront gateway, which signals
// exhaustion via HTTP 402 and visitor rate-limiting via HTTP 429. The widget no
// longer talks to a separate billing API.

/**
 * Response from the brain storefront try-on gateway:
 *   POST {BRAIN_URL}/api/storefront/tryon
 */
export interface TryOnSyncResponse {
	image: string; // Base64-encoded PNG
	cached: boolean;
}

export interface ProductVariantInput {
	variant_id: string;
	size_label: string;
	available: boolean;
}

export interface TargetingRule {
	mode: 'all' | 'include' | 'exclude';
	categories: string[];
	productIds: string[];
}

export interface WidgetSizeGuideInfo {
	id: string;
	categories: { categories: string[]; productIds?: string[] };
	labels: string[];
}

export interface WidgetSizeConfig {
	hasSizeGuide?: boolean;
	guides?: WidgetSizeGuideInfo[];
	targeting?: TargetingRule;
	fitPreferenceEnabled?: boolean;
	defaultFitPreference?: 'tight' | 'regular' | 'loose';
	fitOverrides?: Array<{ categoryId: string; categoryLabel: string; preference: 'tight' | 'regular' | 'loose'; priority: number }>;
}

export interface AvatarData {
	imageData: string; // Base64 image
	createdAt: string;
}


/**
 * window.AISTHETIX_CONFIG injected by the WordPress plugin.
 * Kept compatible with the existing global the portable widget reads.
 */
export interface WidgetConfig {
	/** Brain storefront try-on endpoint, e.g. https://api.aisthetix.app/api/storefront/tryon */
	apiBaseUrl: string;
	sizeApiUrl?: string;
	sizePhotoQcUrl?: string;
	/** Brain base URL, e.g. https://api.aisthetix.app (no trailing slash). */
	brainUrl?: string;
	/** Brain client-event ingestion endpoint, e.g. {brainUrl}/api/events. */
	eventsUrl?: string;
	/** Domain-restricted publishable key — safe for the browser. */
	publishableKey: string;
	/** Merchant attribution opt-in used only by the bootstrap consent gate. */
	analyticsEnabled?: boolean;
	/** Garment image URL (mutated by the bootstrap on variation change). */
	productImage: string;
	productTitle: string;
	/** Verified WordPress site name used only on locally composed share assets. */
	siteName?: string;
	productId: string;
	/** Live WooCommerce variation id, synchronized by the bootstrap. */
	variantId?: string | null;
	/** Product category slugs (provider routing). Empty for uncategorized products. */
	productCategories?: string[];
	/** Stable `wc:product_cat:<term_id>` identifiers used only for fit overrides. */
	productCategoryIds?: string[];
	productVariants?: ProductVariantInput[];
	variationSelections?: Record<string, Record<string, string>>;
	size?: WidgetSizeConfig;
	siteUrl?: string;
	/** WooCommerce site locale, e.g. "it_IT". */
	locale?: string;
	/** Logged-in customer id, or null for guests. */
	customerId?: string | null;
	/**
	 * Durable first-party visitor key (the plugin's aisthetix_vid cookie) — the
	 * PRIMARY attribution key. Falls back to the localStorage visitor id when absent.
	 */
	visitorKey?: string | null;
	/** Shopper-safe copy for the short quota/status cache race. */
	unavailableMessage?: {
		title?: string;
		message?: string;
		buttonLabel?: string;
		buttonUrl?: string;
	};
	/** variationId -> image URL map for variable products. */
	variationImages?: Record<string, string>;
	/** Product page permalink. Used by the demo gallery and the share fallbacks. */
	productUrl?: string;

	// --- Demo-only. Never set on a merchant store. ---------------------------
	/**
	 * Turns on the aisthetix demo customizations (garment PiP, gallery, remaining
	 * counter, email gate). Only the demo store's own mu-plugin sets this, through
	 * the plugin's public `aisthetix_tryon_config` filter; the plugin itself never
	 * emits it, so every merchant install runs with all of it inert.
	 */
	demo?: boolean;
	/** How many try-ons the demo offers a visitor, for the remaining counter. */
	demoTryOnLimit?: number;
	/** Where the demo posts a captured lead. Absent disables the email gate. */
	demoLeadUrl?: string;
}

/**
 * One saved try-on, kept in the shopper's own browser (IndexedDB) and never
 * sent anywhere. Demo-only.
 */
export interface TryOnHistoryItem {
	id: string;
	/** The render, as a data URL. */
	resultImage: string;
	/** The garment, shown as the PiP thumbnail. */
	clothingImage?: string;
	productTitle?: string;
	productId?: string;
	productUrl?: string;
	merchantName?: string;
	merchantDomain?: string;
	/** ISO timestamp; also the gallery's newest-first sort key. */
	createdAt: string;
}

/**
 * The kind of hard-stop the gateway reported, used to choose the blocked view.
 */
export type BlockedReason = 'quota_exhausted' | 'visitor_rate_limited';

export type WidgetState =
	| 'idle'
	| 'avatar-upload'
	| 'processing'
	| 'completed'
	| 'error'
	| 'quota-exceeded'
	| 'visitor-rate-limited'
	| 'size-only'
	| 'size-unavailable'
	/** Demo-only: the saved try-ons. */
	| 'gallery'
	/** Demo-only: the email asked for from the second try-on on. */
	| 'lead-gate';

declare global {
  interface Window { AISTHETIX_SHARE_LANDING?: boolean }
}
