'use client'

import React, { useState, useEffect } from 'react'
import Link from 'next/link'

interface ProductionLineItem {
  id: string
  name: string
  description: string
  station_count: number
  created_at: string
  updated_at: string
}

export default function ProductionLinesListPage() {
  const [lines, setLines] = useState<ProductionLineItem[]>([])
  const [loading, setLoading] = useState(true)

  useEffect(() => {
    fetchLines()
  }, [])

  async function fetchLines() {
    try {
      const res = await fetch('/api/sdk/v1/iace/production-lines')
      if (res.ok) {
        const json = await res.json()
        setLines(json.lines || json || [])
      }
    } catch (err) {
      console.error('Failed to fetch production lines:', 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>
    )
  }

  return (
    <div className="space-y-6 max-w-6xl mx-auto">
      {/* Header */}
      <div className="flex items-start justify-between">
        <div>
          <div className="flex items-center gap-2 mb-1">
            <Link
              href="/sdk/iace"
              className="text-xs text-purple-600 hover:text-purple-700 font-medium flex items-center gap-1"
            >
              <svg className="w-3 h-3" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 19l-7-7 7-7" />
              </svg>
              IACE
            </Link>
          </div>
          <h1 className="text-2xl font-bold text-gray-900 dark:text-white">
            Produktionslinien
          </h1>
          <p className="mt-1 text-sm text-gray-500 dark:text-gray-400">
            Verkettete Fertigungsstrassen mit aggregierter Risikoansicht
          </p>
        </div>
        <Link
          href="/sdk/iace/lines/new"
          className="flex items-center gap-2 px-4 py-2 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors flex-shrink-0"
        >
          <svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 6v6m0 0v6m0-6h6m-6 0H6" />
          </svg>
          Neue Produktionslinie
        </Link>
      </div>

      {/* Lines list */}
      {lines.length > 0 ? (
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
          {lines.map((line) => (
            <Link
              key={line.id}
              href={`/sdk/iace/lines/${line.id}`}
              className="block bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-6 hover:shadow-md hover:border-purple-300 transition-all"
            >
              <h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-1">
                {line.name}
              </h3>
              {line.description && (
                <p className="text-sm text-gray-500 dark:text-gray-400 mb-3 line-clamp-2">
                  {line.description}
                </p>
              )}
              <div className="flex items-center gap-4 text-xs text-gray-500 dark:text-gray-400">
                <span className="flex items-center gap-1">
                  <svg className="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
                  </svg>
                  {line.station_count} Stationen
                </span>
                <span>
                  Aktualisiert: {new Date(line.updated_at || line.created_at).toLocaleDateString('de-DE')}
                </span>
              </div>
            </Link>
          ))}
        </div>
      ) : (
        <div className="bg-white dark:bg-gray-800 rounded-xl border border-gray-200 dark:border-gray-700 p-12 text-center">
          <div className="w-16 h-16 mx-auto bg-purple-100 dark:bg-purple-900/30 rounded-full flex items-center justify-center mb-4">
            <svg className="w-8 h-8 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10" />
            </svg>
          </div>
          <h3 className="text-lg font-semibold text-gray-900 dark:text-white">
            Noch keine Produktionslinien vorhanden
          </h3>
          <p className="mt-2 text-gray-500 dark:text-gray-400 max-w-lg mx-auto">
            Produktionslinien verketten mehrere CE-Projekte zu einer Fertigungsstrasse.
            Sie sehen auf einen Blick den Risikostatus aller Stationen und koennen
            Massnahmen priorisieren.
          </p>
          <Link
            href="/sdk/iace/lines/new"
            className="inline-block mt-6 px-6 py-3 bg-purple-600 text-white rounded-lg hover:bg-purple-700 transition-colors font-medium"
          >
            Erste Produktionslinie erstellen
          </Link>
        </div>
      )}
    </div>
  )
}
