'use client'

import React, { useState, useEffect } from 'react'
import Link from 'next/link'
import { useParams } from 'next/navigation'
import { SuggestedNorms } from './_components/SuggestedNorms'
import { ComplianceAlerts } from './_components/ComplianceAlerts'
import { VariantPanel } from './_components/VariantPanel'

interface ProjectOverview {
  id: string
  machine_name: string
  machine_type: string
  manufacturer: string
  status: string
  completeness_pct: number
  created_at: string
  updated_at: string
  parent_project_id?: string | null
  parent_project_name?: string
  metadata?: { limits_form?: Record<string, unknown> }
  risk_summary?: {
    critical?: number
    high?: number
    medium?: number
    low?: number
    total?: number
  }
  component_count: number
  hazard_count: number
  mitigation_count: number
}

const QUICK_ACTIONS = [
  { href: '/components', label: 'Komponenten verwalten', icon: 'cube', description: 'SW/FW/AI/HMI Baum bearbeiten' },
  { href: '/classification', label: 'Klassifikation pruefen', icon: 'tag', description: 'AI Act, MVO, CRA, NIS2' },
  { href: '/hazards', label: 'Hazard Log oeffnen', icon: 'warning', description: 'Gefaehrdungen und Risiken' },
  { href: '/mitigations', label: 'Massnahmen planen', icon: 'shield', description: 'Design, Schutz, Information' },
  { href: '/verification', label: 'Verifikationsplan', icon: 'check', description: 'Nachweise zuordnen' },
  { href: '/evidence', label: 'Nachweise hochladen', icon: 'document', description: 'Dokumente und Berichte' },
  { href: '/tech-file', label: 'CE-Akte generieren', icon: 'folder', description: 'Technische Dokumentation' },
  { href: '/monitoring', label: 'Monitoring', icon: 'activity', description: 'Post-Market Ueberwachung' },
]

function RiskGauge({ label, value, max, color }: { label: string; value: number; max: number; color: string }) {
  const pct = max > 0 ? Math.round((value / max) * 100) : 0
  return (
    <div className="text-center">
      <div className="relative w-20 h-20 mx-auto">
        <svg className="w-full h-full transform -rotate-90" viewBox="0 0 36 36">
          <path
            d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
            fill="none"
            stroke="#E5E7EB"
            strokeWidth="3"
          />
          <path
            d="M18 2.0845 a 15.9155 15.9155 0 0 1 0 31.831 a 15.9155 15.9155 0 0 1 0 -31.831"
            fill="none"
            stroke={color}
            strokeWidth="3"
            strokeDasharray={`${pct}, 100`}
          />
        </svg>
        <div className="absolute inset-0 flex items-center justify-center">
          <span className="text-lg font-bold text-gray-900 dark:text-white">{value}</span>
        </div>
      </div>
      <div className="mt-1 text-xs text-gray-500">{label}</div>
    </div>
  )
}

const STATUS_WORKFLOW = [
  { key: 'draft', label: 'Entwurf' },
  { key: 'in_progress', label: 'In Bearbeitung' },
  { key: 'review', label: 'In Pruefung' },
  { key: 'approved', label: 'Freigegeben' },
]

export default function ProjectOverviewPage() {
  const params = useParams()
  const projectId = params.projectId as string
  const [project, setProject] = useState<ProjectOverview | null>(null)
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    fetchProject()
  }, [projectId])

  async function fetchProject() {
    try {
      // Only fetch project detail + lightweight risk summary (NO heavy lists)
      const [projRes, riskRes] = await Promise.all([
        fetch(`/api/sdk/v1/iace/projects/${projectId}`),
        fetch(`/api/sdk/v1/iace/projects/${projectId}/risk-summary`),
      ])

      if (!projRes.ok) return
      const json = await projRes.json()

      // Live risk summary from dedicated endpoint (lightweight — just counts)
      let rs = json.risk_summary || {}
      let hazCount = 0
      let mitCount = 0
      if (riskRes.ok) {
        const riskJson = await riskRes.json()
        const live = riskJson.risk_summary || riskJson || {}
        rs = {
          critical: live.critical || 0,
          high: live.high || 0,
          medium: live.medium || 0,
          low: live.low || 0,
          negligible: live.negligible || 0,
          total: live.total_hazards || live.total || 0,
        }
        hazCount = live.total_hazards || live.total || 0
        mitCount = live.total_mitigations || 0
      }

      // Calculate dynamic completeness percentage from CE process steps
      const compCount = json.components?.length || 0
      const limitsForm = json.metadata?.limits_form || {}
      const hasLimits = Object.keys(limitsForm).length > 0
      const hasComponents = compCount > 0
      const hasHazards = hazCount > 0
      const hasMitigations = mitCount > 0
      const stepsComplete = [hasLimits, hasComponents, hasHazards, hasMitigations].filter(Boolean).length
      const completeness = Math.round((stepsComplete / 6) * 100)

      // If this is a variant, resolve parent project name
      let parentName: string | undefined
      if (json.parent_project_id) {
        try {
          const parentRes = await fetch(`/api/sdk/v1/iace/projects/${json.parent_project_id}`)
          if (parentRes.ok) {
            const parentJson = await parentRes.json()
            parentName = parentJson.machine_name
          }
        } catch { /* ignore */ }
      }

      setProject({
        ...json,
        completeness_pct: completeness,
        component_count: compCount,
        hazard_count: hazCount,
        mitigation_count: mitCount,
        parent_project_id: json.parent_project_id || null,
        parent_project_name: parentName,
        metadata: json.metadata,
        risk_summary: {
          critical: rs.critical || 0,
          high: rs.high || 0,
          medium: rs.medium || 0,
          low: rs.low || 0,
          total: rs.total || hazCount,
        },
      })
    } catch (err) {
      console.error('Failed to fetch project:', err)
    } finally {
      setLoading(false)
    }
  }

  if (loading) {
    return (
      <div className="flex items-center justify-center h-64">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-purple-600" />
      </div>
    )
  }

  if (!project) {
    return (
      <div className="text-center py-12">
        <h2 className="text-lg font-semibold text-gray-900 dark:text-white">Projekt nicht gefunden</h2>
        <Link href="/sdk/iace" className="mt-2 text-purple-600 hover:text-purple-700">
          Zurueck zur Uebersicht
        </Link>
      </div>
    )
  }

  const currentStatusIndex = STATUS_WORKFLOW.findIndex((s) => s.key === project.status)

  return (
    <div className="space-y-6">
      {/* Header */}
      <div>
        <h1 className="text-2xl font-bold text-gray-900 dark:text-white">{project.machine_name}</h1>
        <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
          {project.machine_type} {project.manufacturer ? `-- ${project.manufacturer}` : ''}
        </p>
      </div>

      {/* Status Workflow */}
      <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
        <h2 className="text-sm font-semibold text-gray-900 dark:text-white mb-4">Projektstatus</h2>
        <div className="flex items-center gap-2">
          {STATUS_WORKFLOW.map((step, index) => (
            <React.Fragment key={step.key}>
              <div
                className={`flex items-center gap-2 px-4 py-2 rounded-lg text-sm font-medium ${
                  index <= currentStatusIndex
                    ? 'bg-purple-100 text-purple-700 dark:bg-purple-900/50 dark:text-purple-300'
                    : 'bg-gray-100 text-gray-400 dark:bg-gray-700 dark:text-gray-500'
                }`}
              >
                {index < currentStatusIndex ? (
                  <svg className="w-4 h-4 text-purple-600" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
                  </svg>
                ) : index === currentStatusIndex ? (
                  <div className="w-2 h-2 rounded-full bg-purple-600" />
                ) : (
                  <div className="w-2 h-2 rounded-full bg-gray-300" />
                )}
                {step.label}
              </div>
              {index < STATUS_WORKFLOW.length - 1 && (
                <div className={`flex-1 h-0.5 ${index < currentStatusIndex ? 'bg-purple-300' : 'bg-gray-200'}`} />
              )}
            </React.Fragment>
          ))}
        </div>
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {/* Machine Info */}
        <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
          <h2 className="text-sm font-semibold text-gray-900 dark:text-white mb-4">Maschineninformationen</h2>
          <dl className="space-y-3">
            <div>
              <dt className="text-xs text-gray-500">Maschinenname</dt>
              <dd className="text-sm font-medium text-gray-900 dark:text-white">{project.machine_name}</dd>
            </div>
            <div>
              <dt className="text-xs text-gray-500">Typ</dt>
              <dd className="text-sm font-medium text-gray-900 dark:text-white">{project.machine_type || '--'}</dd>
            </div>
            <div>
              <dt className="text-xs text-gray-500">Hersteller</dt>
              <dd className="text-sm font-medium text-gray-900 dark:text-white">{project.manufacturer || '--'}</dd>
            </div>
            <div>
              <dt className="text-xs text-gray-500">Vollstaendigkeit</dt>
              <dd className="mt-1">
                <div className="flex items-center gap-2">
                  <div className="flex-1 bg-gray-200 rounded-full h-2">
                    <div
                      className="bg-purple-500 h-2 rounded-full transition-all"
                      style={{ width: `${project.completeness_pct}%` }}
                    />
                  </div>
                  <span className="text-sm font-medium text-gray-600">{project.completeness_pct}%</span>
                </div>
              </dd>
            </div>
          </dl>
        </div>

        {/* Risk Summary — live from /risk-summary endpoint */}
        <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
          <h2 className="text-sm font-semibold text-gray-900 dark:text-white mb-4">Risikozusammenfassung</h2>
          {/* Risk level bars */}
          <div className="space-y-2">
            {[
              { label: 'Kritisch', value: project.risk_summary?.critical || 0, color: 'bg-red-500', text: 'text-red-700' },
              { label: 'Hoch', value: project.risk_summary?.high || 0, color: 'bg-orange-500', text: 'text-orange-700' },
              { label: 'Mittel', value: project.risk_summary?.medium || 0, color: 'bg-yellow-500', text: 'text-yellow-700' },
              { label: 'Niedrig', value: project.risk_summary?.low || 0, color: 'bg-green-500', text: 'text-green-700' },
            ].map((level) => {
              const total = project.risk_summary?.total || 1
              const pct = Math.round((level.value / total) * 100)
              return (
                <div key={level.label} className="flex items-center gap-3">
                  <span className={`text-xs font-medium w-16 ${level.text}`}>{level.label}</span>
                  <div className="flex-1 bg-gray-100 rounded-full h-4 overflow-hidden">
                    <div className={`${level.color} h-4 rounded-full transition-all`} style={{ width: `${pct}%` }} />
                  </div>
                  <span className="text-sm font-bold text-gray-900 dark:text-white w-8 text-right">{level.value}</span>
                </div>
              )
            })}
          </div>
          {/* Counts */}
          <div className="mt-4 pt-4 border-t border-gray-200 dark:border-gray-700 grid grid-cols-3 gap-4 text-center">
            <div>
              <div className="text-2xl font-bold text-gray-900 dark:text-white">{project.component_count}</div>
              <div className="text-xs text-gray-500">Komponenten</div>
            </div>
            <div>
              <div className="text-2xl font-bold text-gray-900 dark:text-white">{project.hazard_count}</div>
              <div className="text-xs text-gray-500">Gefaehrdungen</div>
            </div>
            <div>
              <div className="text-2xl font-bold text-gray-900 dark:text-white">{project.mitigation_count}</div>
              <div className="text-xs text-gray-500">Massnahmen</div>
            </div>
          </div>
          {/* RPZ threshold info */}
          <div className="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700 text-xs text-gray-500">
            RPZ-Schwellen: Kritisch &gt;100 | Hoch 60-100 | Mittel 20-60 | Niedrig &lt;20
          </div>
        </div>

        {/* Progress Tracker */}
        <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6">
          <div className="flex items-center justify-between mb-3">
            <h2 className="text-sm font-semibold text-gray-900 dark:text-white">Projektfortschritt</h2>
            <span className="text-sm font-bold text-purple-600">{project.completeness_pct}%</span>
          </div>
          <div className="w-full bg-gray-200 rounded-full h-2.5 mb-4">
            <div className="bg-purple-500 h-2.5 rounded-full transition-all" style={{ width: `${project.completeness_pct}%` }} />
          </div>
          <div className="space-y-2">
            {(() => {
              const hasLimits = Object.keys(project.metadata?.limits_form || {}).length > 0
              const steps = [
                { done: hasLimits, label: 'Grenzen definiert', detail: hasLimits ? 'Felder ausgefuellt' : 'ausstehend' },
                { done: project.component_count > 0, label: 'Komponenten erfasst', detail: `${project.component_count} Komponenten` },
                { done: project.hazard_count > 0, label: 'Gefaehrdungen identifiziert', detail: `${project.hazard_count} bewertet` },
                { done: project.mitigation_count > 0, label: 'Massnahmen zugeordnet', detail: `${project.mitigation_count} Massnahmen` },
                { done: false, label: 'Verifikation', detail: 'ausstehend' },
                { done: false, label: 'CE-Akte', detail: 'ausstehend' },
              ]
              const firstPending = steps.find((s) => !s.done)
              return (
                <>
                  {steps.map((step) => (
                    <div key={step.label} className="flex items-center gap-2">
                      <span className={`text-sm flex-shrink-0 ${step.done ? 'text-green-600' : 'text-gray-400'}`}>
                        {step.done ? '\u2713' : '\u25CB'}
                      </span>
                      <span className={`text-sm flex-1 ${step.done ? 'text-gray-900 dark:text-white' : 'text-gray-400'}`}>{step.label}</span>
                      <span className="text-xs text-gray-400">{step.detail}</span>
                    </div>
                  ))}
                  <div className="mt-3 pt-3 border-t border-gray-200 dark:border-gray-700 text-xs text-gray-500">
                    Naechster Schritt: {firstPending?.label || 'Alle Schritte abgeschlossen'}
                  </div>
                </>
              )
            })()}
          </div>
        </div>
      </div>

      {/* Safety Disclaimer */}
      <div className="p-3 rounded-lg bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 text-xs text-amber-800 dark:text-amber-300">
        <strong>Hinweis:</strong> Automatisch erkannte Gefaehrdungen, Massnahmen und SIL/PL-Einschaetzungen sind eine qualifizierte Ausgangsbasis.
        Vollstaendigkeit und Angemessenheit muessen vom CE-Verantwortlichen des Herstellers validiert werden.
        Dieses Tool ersetzt nicht die Pflichten des Herstellers nach EU 2023/1230 Art. 10.
      </div>

      {/* Compliance Alerts */}
      <ComplianceAlerts projectId={projectId} />

      {/* Variant Management */}
      <VariantPanel
        projectId={projectId}
        parentProjectId={project.parent_project_id}
        parentProjectName={project.parent_project_name}
      />

      {/* Suggested Norms */}
      <SuggestedNorms projectId={projectId} />

      {/* Quick Actions */}
      <div>
        <h2 className="text-sm font-semibold text-gray-900 dark:text-white mb-4">Schnellzugriff</h2>
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-3">
          {QUICK_ACTIONS.map((action) => (
            <Link
              key={action.href}
              href={`/sdk/iace/${projectId}${action.href}`}
              className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-4 hover:shadow-md hover:border-purple-300 transition-all group"
            >
              <div className="flex items-center gap-3">
                <div className="w-10 h-10 bg-purple-50 dark:bg-purple-900/30 rounded-lg flex items-center justify-center text-purple-600 group-hover:bg-purple-100 transition-colors flex-shrink-0">
                  <svg className="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 7l5 5m0 0l-5 5m5-5H6" />
                  </svg>
                </div>
                <div className="min-w-0">
                  <div className="text-sm font-medium text-gray-900 dark:text-white truncate">{action.label}</div>
                  <div className="text-xs text-gray-500 truncate">{action.description}</div>
                </div>
              </div>
            </Link>
          ))}
        </div>
      </div>
    </div>
  )
}
