// ─── Katalog (gelesen aus Supabase) ──────────────────────────────────────────

export type BillingInterval = 'one_time' | 'monthly'

export type Product = {
  id: string
  name: string
  description?: string | null
  base_price: number
  tax_rate: number
  billing_interval: BillingInterval
  category_id?: string | null
  category?: Category | null
  modules?: ProductModule[]
  created_at: string
  show_in_kauf?: boolean
  show_in_abo?: boolean
  requirements?: string[]
  exclusions?: string[]
}

export type Category = {
  id: string
  name: string
  description?: string | null
  icon?: string | null
  sort_order: number
  is_required: boolean
  created_at: string
}

export type ProductModule = {
  id: string
  product_id: string
  name: string
  description?: string | null
  price: number
  requirements: string[]   // UUID[] anderer Module, die aktiv sein müssen
  exclusions: string[]     // UUID[] von Modulen, die nicht gleichzeitig aktiv sein dürfen
  created_at: string
  has_quantity?: boolean
}

export type Profile = {
  id: string
  company_name: string | null
  vat_id: string | null
  first_name: string | null
  last_name: string | null
  address: string | null
  city: string | null
  zip_code: string | null
  country: string
  email?: string | null
  updated_at: string
}

// ─── Endkunde (verwaltet vom Partner) ────────────────────────────────────────

export type EndCustomer = {
  id: string
  partner_id: string
  company_name: string
  vat_id: string | null
  first_name: string | null
  last_name: string | null
  street: string | null
  zip: string | null
  city: string | null
  email?: string | null
  is_anonymized: boolean
  created_at: string
}

// ─── Wizard-Zustand (flüchtig, im RAM) ───────────────────────────────────────

export type CategorySelection = {
  productId: string | null
  moduleIds: string[]
}

/** key = category_id */
export type WizardSelections = Record<string, CategorySelection>

// ─── Versioned Snapshots (unveränderlich in JSONB gespeichert) ────────────────

/**
 * Kundendaten zum Zeitpunkt der Bestellung.
 * schema_version erlaubt spätere Migrationsfunktionen ohne DB-Änderung.
 */
export type CustomerSnapshot = {
  schema_version: 1
  /** ID des Endkunden zum Zeitpunkt der Bestellung (für Audit-Trail) */
  end_customer_id?: string
  company_name: string
  vat_id: string
  first_name: string
  last_name: string
  address: string
  zip_code: string
  city: string
  email?: string
}

export type OrderModuleSnapshot = {
  module_id: string
  module_name: string
  price: number
  quantity?: number
  total_price?: number
}

export type OrderItem = {
  category_id: string
  category_name: string
  product_id: string
  product_name: string
  base_price: number
  billing_interval: BillingInterval
  selected_modules: OrderModuleSnapshot[]
  item_total: number
}

/**
 * Vollständiger Bestell-Snapshot – wird in orders.order_data (JSONB) gespeichert.
 */
export type OrderSnapshot = {
  schema_version: 1
  billing_cycle: BillingInterval
  items: OrderItem[]
  subtotal: number
  tax_rate: number
  tax_amount: number
  total: number
}

// ─── DB-Zeile (orders-Tabelle) ────────────────────────────────────────────────

export type Order = {
  id: string
  user_id: string | null
  order_number: string
  order_hash: string | null
  end_customer_id: string | null
  customer_data: CustomerSnapshot
  order_data: OrderSnapshot
  total_price: number
  pdf_url: string | null
  status: 'pending' | 'active' | 'cancelled'
  created_at: string
}

// ─── Lizenz-Output (für Lizenzserver / ERP) ───────────────────────────────────

export type LicenseOption = {
  /** Stabiler Slug-Key, z. B. "caspos_basic__backoffice" */
  key: string
  label: string
  active: boolean
  billing: BillingInterval
  price_net: number
  quantity?: number
}

export type LicenseBundle = {
  customer_ref: string
  generated_at: string
  license_key_prefix: string
  options: LicenseOption[]
  monthly_total_net: number
  one_time_total_net: number
}

