/**
 * Commercial Runtime types — commercial.v1.2 + launch metadata.
 * UI never invents registry rows; adapters populate from Core Commercial APIs.
 */

export type RegistryAvailability =
  | "loading"
  | "ready"
  | "empty"
  | "unavailable"
  | "permission_denied"
  | "error";

export interface AdapterResult<T> {
  availability: RegistryAvailability;
  data: T;
  message?: string;
  source?: string;
}

export type LaunchStrategy = "route" | "external" | "embed" | "disabled" | string;

export interface CommercialProduct {
  product_code: string;
  display_name: string;
  description?: string | null;
  icon?: string | null;
  color?: string | null;
  status?: string | null;
  version?: string | null;
  visibility?: string | null;
  /** @deprecated prefer default_route */
  href?: string | null;
  product_slug?: string | null;
  default_route?: string | null;
  category?: string | null;
  display_order?: number | null;
  launch_strategy?: LaunchStrategy | null;
  dependencies?: string[];
  required_capabilities?: string[];
  recommended_industries?: string[];
  pricing_refs?: string[];
  trial_eligible?: boolean;
  service_key?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialBundle {
  bundle_code: string;
  display_name: string;
  description?: string | null;
  product_codes?: string[];
  capability_codes?: string[];
  pricing_refs?: string[];
  discount_refs?: string[];
  recommendation_badge?: string | null;
  industry_codes?: string[];
  trial_eligible?: boolean;
  upgrade_path_code?: string | null;
  feature_refs?: string[];
  metadata?: Record<string, unknown>;
}

export interface CommercialPricingItem {
  pricing_item_code: string;
  display_name: string;
  pricing_model?: string | null;
  currency?: string | null;
  amount_minor?: number | null;
  region_code?: string | null;
  tax_class_code?: string | null;
  trial_days?: number | null;
  status?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialCapability {
  capability_code: string;
  display_name: string;
  description?: string | null;
  status?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialAsset {
  asset_code: string;
  display_name: string;
  asset_type_code?: string | null;
  status?: string | null;
  version_code?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialExtension {
  extension_code: string;
  display_name: string;
  extension_kind?: string | null;
  publisher_product_code?: string | null;
  status?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialAutomationPack {
  automation_pack_code: string;
  display_name: string;
  automation_type_codes?: string[];
  status?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialPolicy {
  policy_code: string;
  display_name: string;
  policy_kind?: string | null;
  status?: string | null;
  effect?: string | null;
  parameters?: Record<string, unknown>;
  metadata?: Record<string, unknown>;
}

export interface CommercialMetadataBag {
  registry_object_id?: string;
  target_kind?: string;
  target_id?: string;
  attributes: Record<string, unknown>;
  version_code?: string;
}

export interface CommercialNotification {
  id: string;
  title: string;
  body?: string | null;
  severity?: string | null;
  priority?: string | null;
  href?: string | null;
  read?: boolean;
  created_at?: string | null;
  metadata?: Record<string, unknown>;
}

export interface CommercialUsageCounter {
  key: string;
  label?: string | null;
  used?: number | null;
  limit?: number | null;
  unit?: string | null;
  metadata?: Record<string, unknown>;
}

export interface AssessmentQuestionOption {
  value: string;
  label: string;
  score_delta?: number;
  product_hints?: string[];
  capability_hints?: string[];
  bundle_hints?: string[];
}

export interface AssessmentQuestion {
  question_id: string;
  prompt: string;
  input_type: "single" | "multi" | "boolean" | "number" | "text" | string;
  required?: boolean;
  options?: AssessmentQuestionOption[];
  metadata?: Record<string, unknown>;
}

export interface AssessmentSchema {
  schema_version: string;
  title?: string;
  questions: AssessmentQuestion[];
}

export interface RecommendationResult {
  recommended_products: Array<{ code: string; score: number; reason?: string }>;
  recommended_bundles: Array<{ code: string; score: number; reason?: string }>;
  recommended_capabilities: Array<{ code: string; score: number; reason?: string }>;
  recommended_pricing: Array<{ code: string; score: number; reason?: string }>;
  rule_trace?: string[];
}

export interface TrialState {
  active: boolean;
  days_total?: number | null;
  days_remaining?: number | null;
  ends_at?: string | null;
  quotas?: CommercialUsageCounter[];
}

export interface DomainPolicyResult {
  custom_domain_allowed: boolean;
  subdomain_only: boolean;
  reason?: string | null;
  parameters?: Record<string, unknown>;
  source?: string;
  ssl_ready?: boolean;
  dns_instructions?: string[] | Array<{ host?: string; type?: string; value?: string; note?: string }>;
  verification_state?: string | null;
  grace_period_days?: number | null;
}

export interface LockedFeatureHint {
  feature_key: string;
  reason?: string | null;
  required_plan?: string | null;
  required_capability?: string | null;
  required_bundle?: string | null;
}

export interface CommercialWorkspaceSnapshot {
  tenant_id: string;
  tenant_name: string;
  slug: string;
  status: string;
  plan_name?: string | null;
  subscription_status?: string | null;
  onboarding_completed?: boolean;
  primary_domain?: string | null;
  domains: Array<{
    id: string;
    domain: string;
    domain_type: string;
    is_verified: boolean;
  }>;
  role?: string | null;
  trial?: TrialState | null;
  bundle_code?: string | null;
  product_codes?: string[];
  capability_codes?: string[];
  custom_domain_unlocked: boolean;
  domain_policy?: DomainPolicyResult | null;
  pinned_product_codes?: string[];
  recent_product_codes?: string[];
  quick_actions?: Array<{ id: string; label: string; href?: string | null }>;
  locked_features?: LockedFeatureHint[];
  workspace_health?: string | null;
  checklist_completion_percent?: number | null;
}

export interface SetupChecklistItem {
  id: string;
  label: string;
  href?: string | null;
  product_code?: string | null;
  done?: boolean;
  required?: boolean;
}

export interface EntitlementCheckResult {
  has_access: boolean;
  reason?: string | null;
  required_plan?: string | null;
  required_capability?: string | null;
  required_bundle?: string | null;
  unavailable?: boolean;
}

/** Resolve launch URL from product runtime metadata only — never FE hub maps. */
export function resolveProductLaunchUrl(product: CommercialProduct): string | null {
  if (product.launch_strategy === "disabled") return null;
  const route = product.default_route || product.href || null;
  if (!route) return null;
  return route;
}
