// ============================================
// TYPES - B2B Alerts Domain Types
// ============================================

export type ImportanceLabel = 'KRITISCH' | 'DRINGEND' | 'WICHTIG' | 'PRUEFEN' | 'INFO'
export type DecisionLabel = 'relevant' | 'irrelevant' | 'needs_review'
export type SourceType = 'email' | 'rss'
export type NoiseMode = 'STRICT' | 'BALANCED' | 'BROAD'
export type Package = 'PARKING' | 'EV_CHARGING' | 'FUEL' | 'TANK_MONITORING'

export interface AlertSource {
  id: string
  tenantId: string
  type: SourceType
  inboundAddress?: string
  rssUrl?: string
  label: string
  active: boolean
  createdAt: Date
}

export interface B2BHit {
  id: string
  tenantId: string
  topicId: string
  sourceType: SourceType
  sourceRef: string
  originalUrl: string
  canonicalUrl: string
  title: string
  snippet: string
  fullText?: string
  foundAt: Date
  language?: string
  countryGuess?: string
  buyerGuess?: string
  deadlineGuess?: string
  importanceScore: number
  importanceLabel: ImportanceLabel
  decisionLabel: DecisionLabel
  decisionConfidence: number
  decisionTrace?: DecisionTrace
  isRead: boolean
  userFeedback?: 'relevant' | 'irrelevant'
  createdAt: Date
}

export interface DecisionTrace {
  rulesTriggered: string[]
  llmUsed: boolean
  llmConfidence?: number
  signals: {
    procurementSignalsFound: string[]
    publicBuyerSignalsFound: string[]
    productSignalsFound: string[]
    negativesFound: string[]
  }
}

export interface B2BTopic {
  id: string
  tenantId: string
  name: string
  package: Package
  intent: TopicIntent
  filters: TopicFilters
  decisionPolicy: DecisionPolicy
  importanceModelId: string
  status: 'active' | 'paused'
  createdAt: Date
}

export interface TopicIntent {
  goalStatement: string
  mustHaveAnyN: number
  mustHave: { type: 'keyword'; value: string }[]
  publicBuyerSignalsAny: string[]
  procurementSignalsAny: string[]
}

export interface TopicFilters {
  hardExcludesAny: string[]
  softExcludesAny: string[]
}

export interface DecisionPolicy {
  rulesFirst: boolean
  llmMode: 'ALWAYS' | 'GRAYZONE_ONLY' | 'NEVER'
  autoRelevantMinConf: number
  needsReviewRange: [number, number]
  autoIrrelevantMaxConf: number
}

export interface ImportanceModel {
  modelId: string
  scoreRange: [number, number]
  weights: {
    deadlineProximity: number
    procurementIntentStrength: number
    publicBuyerStrength: number
    productMatchStrength: number
    sourceTrust: number
    novelty: number
  }
  thresholds: {
    infoMax: number
    reviewMin: number
    importantMin: number
    urgentMin: number
    criticalMin: number
  }
  topNCaps: {
    dailyDigestMax: number
    weeklyDigestMax: number
    perTopicDailyMax: number
  }
}

export interface B2BTemplate {
  templateId: string
  templateName: string
  templateDescription: string
  targetRoles: string[]
  industry?: string
  companyExample?: string
  topics: Omit<B2BTopic, 'id' | 'tenantId' | 'createdAt'>[]
  importanceModel: ImportanceModel
  guidedConfig: GuidedConfig
  notificationPreset: NotificationPreset
}

export interface GuidedConfig {
  regionSelector: {
    type: 'multi_select'
    default: string[]
    options: string[]
  }
  languageSelector: {
    type: 'multi_select'
    default: string[]
    options: string[]
  }
  packageSelector: {
    type: 'multi_select'
    default: Package[]
    options: Package[]
  }
  noiseMode: {
    type: 'single_select'
    default: NoiseMode
    options: NoiseMode[]
  }
}

export interface NotificationPreset {
  presetId: string
  channels: string[]
  cadence: 'REALTIME' | 'HOURLY' | 'DAILY' | 'WEEKLY'
  timeLocal: string
  includeStatuses: DecisionLabel[]
  maxItems: number
}

export interface B2BTenant {
  id: string
  name: string
  companyName: string
  industry: string
  plan: 'trial' | 'starter' | 'professional' | 'enterprise'
  inboundEmailDomain: string
  createdAt: Date
  settings: B2BSettings
}

export interface B2BSettings {
  migrationCompleted: boolean
  wizardCompleted: boolean
  selectedTemplateId?: string
  selectedRegions: string[]
  selectedLanguages: string[]
  selectedPackages: Package[]
  noiseMode: NoiseMode
  notificationCadence: 'REALTIME' | 'HOURLY' | 'DAILY' | 'WEEKLY'
  notificationTime: string
  maxDigestItems: number
}

export interface AlertsB2BContextType {
  // Tenant
  tenant: B2BTenant
  updateTenant: (updates: Partial<B2BTenant>) => void

  // Settings
  settings: B2BSettings
  updateSettings: (updates: Partial<B2BSettings>) => void

  // Templates
  availableTemplates: B2BTemplate[]
  selectedTemplate: B2BTemplate | null
  selectTemplate: (templateId: string) => void

  // Sources
  sources: AlertSource[]
  addSource: (source: Omit<AlertSource, 'id' | 'createdAt'>) => void
  removeSource: (id: string) => void
  generateInboundEmail: () => string

  // Topics
  topics: B2BTopic[]
  addTopic: (topic: Omit<B2BTopic, 'id' | 'createdAt'>) => void
  updateTopic: (id: string, updates: Partial<B2BTopic>) => void
  removeTopic: (id: string) => void

  // Hits
  hits: B2BHit[]
  unreadCount: number
  relevantCount: number
  needsReviewCount: number
  markAsRead: (id: string) => void
  markAllAsRead: () => void
  submitFeedback: (id: string, feedback: 'relevant' | 'irrelevant') => void
  processEmailContent: (emailContent: string, emailSubject?: string) => B2BHit

  // Digest
  getDigest: (maxItems?: number) => B2BHit[]

  // Loading/Error
  isLoading: boolean
  error: string | null
}
