export interface Portfolio {
  id: string
  name: string
  description: string
  status: 'DRAFT' | 'ACTIVE' | 'REVIEW' | 'APPROVED' | 'ARCHIVED'
  department: string
  business_unit: string
  owner: string
  owner_email: string
  total_assessments: number
  total_roadmaps: number
  total_workshops: number
  avg_risk_score: number
  high_risk_count: number
  compliance_score: number
  auto_update_metrics: boolean
  require_approval: boolean
  created_at: string
  updated_at: string
  approved_at: string | null
  approved_by: string | null
}

export interface PortfolioItem {
  id: string
  portfolio_id: string
  item_type: 'ASSESSMENT' | 'ROADMAP' | 'WORKSHOP' | 'DOCUMENT'
  item_id: string
  title: string
  status: string
  risk_level: string
  risk_score: number
  feasibility: string
  sort_order: number
  tags: string[]
  notes: string
  created_at: string
}

export interface PortfolioStats {
  total_items: number
  items_by_type: Record<string, number>
  risk_distribution: Record<string, number>
  avg_risk_score: number
  compliance_score: number
}

export interface ActivityEntry {
  timestamp: string
  action: string
  item_type: string
  item_id: string
  item_title: string
  user_id: string
}

export interface CompareResult {
  portfolios: Portfolio[]
  risk_scores: Record<string, number>
  compliance_scores: Record<string, number>
  item_counts: Record<string, number>
  common_items: string[]
  unique_items: Record<string, string[]>
}

export const API_BASE = '/api/sdk/v1/portfolio'

export async function api<T>(path: string, options?: RequestInit): Promise<T> {
  const res = await fetch(`${API_BASE}${path}`, {
    headers: { 'Content-Type': 'application/json' },
    ...options,
  })
  if (!res.ok) {
    const err = await res.json().catch(() => ({ error: res.statusText }))
    throw new Error(err.error || err.message || `HTTP ${res.status}`)
  }
  return res.json()
}

export const statusColors: Record<string, string> = {
  DRAFT: 'bg-gray-100 text-gray-700',
  ACTIVE: 'bg-green-100 text-green-700',
  REVIEW: 'bg-yellow-100 text-yellow-700',
  APPROVED: 'bg-purple-100 text-purple-700',
  ARCHIVED: 'bg-red-100 text-red-700',
}

export const statusLabels: Record<string, string> = {
  DRAFT: 'Entwurf',
  ACTIVE: 'Aktiv',
  REVIEW: 'In Pruefung',
  APPROVED: 'Genehmigt',
  ARCHIVED: 'Archiviert',
}
