'use client'

import { useState, useEffect } from 'react'
import {
  GitBranch,
  Terminal,
  Server,
  Database,
  CheckCircle2,
  ArrowRight,
  Laptop,
  HardDrive,
  RefreshCw,
  Clock,
  Shield,
  Users,
  FileCode,
  Play,
  Eye,
  Download,
  AlertTriangle,
  Info
} from 'lucide-react'

interface WorkflowStep {
  id: number
  title: string
  description: string
  command?: string
  icon: React.ReactNode
  location: 'macbook' | 'macmini'
}

interface BackupInfo {
  lastRun: string | null
  nextRun: string
  status: 'ok' | 'warning' | 'error'
}

export default function WorkflowPage() {
  const [activeStep, setActiveStep] = useState<number>(1)
  const [backupInfo, setBackupInfo] = useState<BackupInfo>({
    lastRun: null,
    nextRun: '02:00 Uhr',
    status: 'ok'
  })

  const workflowSteps: WorkflowStep[] = [
    {
      id: 1,
      title: 'Code bearbeiten',
      description: 'Arbeite mit Claude Code im Terminal. Beschreibe was du brauchst und Claude schreibt den Code.',
      command: 'claude',
      icon: <Terminal className="h-6 w-6" />,
      location: 'macbook'
    },
    {
      id: 2,
      title: 'Änderungen stagen',
      description: 'Füge die geänderten Dateien zum nächsten Commit hinzu.',
      command: 'git add <dateien>',
      icon: <FileCode className="h-6 w-6" />,
      location: 'macbook'
    },
    {
      id: 3,
      title: 'Commit erstellen',
      description: 'Erstelle einen Commit mit einer aussagekräftigen Nachricht.',
      command: 'git commit -m "feat: neue Funktion"',
      icon: <GitBranch className="h-6 w-6" />,
      location: 'macbook'
    },
    {
      id: 4,
      title: 'Push zum Server',
      description: 'Sende die Änderungen an den Mac Mini. Dies startet automatisch die CI/CD Pipeline.',
      command: 'git push origin main',
      icon: <ArrowRight className="h-6 w-6" />,
      location: 'macbook'
    },
    {
      id: 5,
      title: 'CI/CD Pipeline',
      description: 'Woodpecker führt automatisch Tests aus und baut die Container.',
      command: '(automatisch)',
      icon: <RefreshCw className="h-6 w-6" />,
      location: 'macmini'
    },
    {
      id: 6,
      title: 'Frontend testen',
      description: 'Teste die Änderungen im Browser auf dem Mac Mini.',
      command: 'http://macmini:3000',
      icon: <Eye className="h-6 w-6" />,
      location: 'macbook'
    }
  ]

  const services = [
    { name: 'Website', url: 'http://macmini:3000', port: 3000, status: 'running' },
    { name: 'Admin v2', url: 'http://macmini:3002', port: 3002, status: 'running' },
    { name: 'Studio v2', url: 'http://macmini:3001', port: 3001, status: 'running' },
    { name: 'Backend', url: 'http://macmini:8000', port: 8000, status: 'running' },
    { name: 'Gitea', url: 'http://macmini:3003', port: 3003, status: 'running' },
    { name: 'Klausur-Service', url: 'http://macmini:8086', port: 8086, status: 'running' },
  ]

  const commitTypes = [
    { type: 'feat:', description: 'Neue Funktion', example: 'feat: add user login' },
    { type: 'fix:', description: 'Bugfix', example: 'fix: resolve login timeout' },
    { type: 'docs:', description: 'Dokumentation', example: 'docs: update API docs' },
    { type: 'style:', description: 'Formatierung', example: 'style: fix indentation' },
    { type: 'refactor:', description: 'Code-Umbau', example: 'refactor: extract helper' },
    { type: 'test:', description: 'Tests', example: 'test: add unit tests' },
    { type: 'chore:', description: 'Wartung', example: 'chore: update deps' },
  ]

  return (
    <div className="space-y-8">
      {/* Header */}
      <div className="bg-gradient-to-r from-indigo-600 to-purple-600 rounded-2xl p-8 text-white">
        <h1 className="text-3xl font-bold mb-2">Entwicklungs-Workflow</h1>
        <p className="text-indigo-100">
          Wie wir bei BreakPilot entwickeln - von der Idee bis zum Deployment
        </p>
      </div>

      {/* Architecture Overview */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-4 flex items-center gap-2">
          <Server className="h-5 w-5 text-indigo-600" />
          Systemarchitektur
        </h2>

        <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
          {/* MacBook */}
          <div className="bg-slate-50 rounded-xl p-5 border-2 border-slate-200">
            <div className="flex items-center gap-3 mb-4">
              <div className="p-2 bg-blue-100 rounded-lg">
                <Laptop className="h-6 w-6 text-blue-600" />
              </div>
              <div>
                <h3 className="font-semibold text-slate-900">MacBook (Entwicklung)</h3>
                <p className="text-sm text-slate-500">Dein Arbeitsplatz</p>
              </div>
            </div>
            <ul className="space-y-2 text-sm">
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Terminal + Claude Code</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Lokales Git Repository</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Browser für Frontend-Tests</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Tägliches Backup (automatisch)</span>
              </li>
            </ul>
          </div>

          {/* Mac Mini */}
          <div className="bg-slate-50 rounded-xl p-5 border-2 border-indigo-200">
            <div className="flex items-center gap-3 mb-4">
              <div className="p-2 bg-indigo-100 rounded-lg">
                <HardDrive className="h-6 w-6 text-indigo-600" />
              </div>
              <div>
                <h3 className="font-semibold text-slate-900">Mac Mini (Server)</h3>
                <p className="text-sm text-slate-500">192.168.178.100</p>
              </div>
            </div>
            <ul className="space-y-2 text-sm">
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Gitea (Git Server)</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Woodpecker (CI/CD)</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>Docker Container (alle Services)</span>
              </li>
              <li className="flex items-center gap-2">
                <CheckCircle2 className="h-4 w-4 text-green-500" />
                <span>PostgreSQL Datenbank</span>
              </li>
            </ul>
          </div>
        </div>
      </div>

      {/* Workflow Steps */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-6 flex items-center gap-2">
          <Play className="h-5 w-5 text-indigo-600" />
          Entwicklungs-Schritte
        </h2>

        <div className="space-y-4">
          {workflowSteps.map((step, index) => (
            <div
              key={step.id}
              className={`relative flex items-start gap-4 p-4 rounded-xl transition-all cursor-pointer ${
                activeStep === step.id
                  ? 'bg-indigo-50 border-2 border-indigo-300'
                  : 'bg-slate-50 border-2 border-transparent hover:border-slate-200'
              }`}
              onClick={() => setActiveStep(step.id)}
            >
              {/* Step Number */}
              <div className={`flex-shrink-0 w-10 h-10 rounded-full flex items-center justify-center font-bold ${
                activeStep === step.id
                  ? 'bg-indigo-600 text-white'
                  : 'bg-slate-200 text-slate-600'
              }`}>
                {step.id}
              </div>

              {/* Content */}
              <div className="flex-grow">
                <div className="flex items-center gap-2 mb-1">
                  <h3 className="font-semibold text-slate-900">{step.title}</h3>
                  <span className={`text-xs px-2 py-0.5 rounded-full ${
                    step.location === 'macbook'
                      ? 'bg-blue-100 text-blue-700'
                      : 'bg-purple-100 text-purple-700'
                  }`}>
                    {step.location === 'macbook' ? 'MacBook' : 'Mac Mini'}
                  </span>
                </div>
                <p className="text-sm text-slate-600 mb-2">{step.description}</p>
                {step.command && (
                  <code className="text-xs bg-slate-800 text-green-400 px-3 py-1.5 rounded-lg font-mono">
                    {step.command}
                  </code>
                )}
              </div>

              {/* Icon */}
              <div className={`flex-shrink-0 p-2 rounded-lg ${
                activeStep === step.id ? 'bg-indigo-100 text-indigo-600' : 'bg-slate-100 text-slate-400'
              }`}>
                {step.icon}
              </div>

              {/* Connector Line */}
              {index < workflowSteps.length - 1 && (
                <div className="absolute left-9 top-14 w-0.5 h-8 bg-slate-200" />
              )}
            </div>
          ))}
        </div>
      </div>

      {/* Services & URLs */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-4 flex items-center gap-2">
          <Eye className="h-5 w-5 text-indigo-600" />
          Services & URLs zum Testen
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3">
          {services.map((service) => (
            <a
              key={service.name}
              href={service.url}
              target="_blank"
              rel="noopener noreferrer"
              className="flex items-center justify-between p-4 bg-slate-50 rounded-lg hover:bg-slate-100 transition-colors border border-slate-200"
            >
              <div>
                <h3 className="font-medium text-slate-900">{service.name}</h3>
                <p className="text-sm text-slate-500">Port {service.port}</p>
              </div>
              <div className="flex items-center gap-2">
                <span className="w-2 h-2 bg-green-500 rounded-full animate-pulse" />
                <ArrowRight className="h-4 w-4 text-slate-400" />
              </div>
            </a>
          ))}
        </div>
      </div>

      {/* Commit Convention */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-4 flex items-center gap-2">
          <GitBranch className="h-5 w-5 text-indigo-600" />
          Commit-Konventionen
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-3">
          {commitTypes.map((item) => (
            <div key={item.type} className="bg-slate-50 rounded-lg p-3 border border-slate-200">
              <code className="text-sm font-bold text-indigo-600">{item.type}</code>
              <p className="text-sm text-slate-600 mt-1">{item.description}</p>
              <p className="text-xs text-slate-400 mt-1 font-mono">{item.example}</p>
            </div>
          ))}
        </div>
      </div>

      {/* Backup Info */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-4 flex items-center gap-2">
          <Shield className="h-5 w-5 text-indigo-600" />
          Backup & Sicherheit
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
          <div className="bg-green-50 rounded-xl p-5 border border-green-200">
            <div className="flex items-center gap-3 mb-3">
              <Clock className="h-5 w-5 text-green-600" />
              <h3 className="font-semibold text-green-900">Tägliches Backup</h3>
            </div>
            <ul className="space-y-2 text-sm text-green-800">
              <li>• Läuft automatisch um 02:00 Uhr</li>
              <li>• Git Repository wird synchronisiert</li>
              <li>• PostgreSQL-Dump wird erstellt</li>
              <li>• Backups werden 7 Tage aufbewahrt</li>
            </ul>
            <div className="mt-4 p-3 bg-green-100 rounded-lg">
              <code className="text-xs text-green-700 font-mono">
                ~/Projekte/backup-logs/
              </code>
            </div>
          </div>

          <div className="bg-blue-50 rounded-xl p-5 border border-blue-200">
            <div className="flex items-center gap-3 mb-3">
              <Download className="h-5 w-5 text-blue-600" />
              <h3 className="font-semibold text-blue-900">Manuelles Backup</h3>
            </div>
            <p className="text-sm text-blue-800 mb-3">
              Backup jederzeit manuell starten:
            </p>
            <code className="block text-xs bg-slate-800 text-green-400 p-3 rounded-lg font-mono">
              ~/Projekte/breakpilot-pwa/scripts/daily-backup.sh
            </code>
          </div>
        </div>
      </div>

      {/* Quick Commands */}
      <div className="bg-slate-800 rounded-xl p-6 text-white">
        <h2 className="text-xl font-semibold mb-4 flex items-center gap-2">
          <Terminal className="h-5 w-5 text-green-400" />
          Wichtige Befehle
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4 font-mono text-sm">
          <div className="bg-slate-900 rounded-lg p-4">
            <p className="text-slate-400 mb-2"># CI/CD Logs ansehen</p>
            <code className="text-green-400">ssh macmini &quot;docker logs breakpilot-pwa-backend --tail 50&quot;</code>
          </div>
          <div className="bg-slate-900 rounded-lg p-4">
            <p className="text-slate-400 mb-2"># Container neu starten</p>
            <code className="text-green-400">ssh macmini &quot;docker compose restart backend&quot;</code>
          </div>
          <div className="bg-slate-900 rounded-lg p-4">
            <p className="text-slate-400 mb-2"># Alle Container Status</p>
            <code className="text-green-400">ssh macmini &quot;docker ps&quot;</code>
          </div>
          <div className="bg-slate-900 rounded-lg p-4">
            <p className="text-slate-400 mb-2"># Pipeline Status (Gitea)</p>
            <code className="text-green-400">open http://macmini:3003</code>
          </div>
        </div>
      </div>

      {/* Team Workflow with Feature Branches */}
      <div className="bg-indigo-50 rounded-xl border border-indigo-200 p-6">
        <h2 className="text-xl font-semibold text-indigo-900 mb-4 flex items-center gap-2">
          <GitBranch className="h-5 w-5 text-indigo-600" />
          Team-Workflow (3+ Entwickler)
        </h2>

        <div className="bg-white rounded-xl p-5 mb-4">
          <h3 className="font-semibold text-slate-900 mb-3">Feature Branch Workflow</h3>
          <div className="flex flex-wrap items-center gap-2 text-sm">
            <code className="bg-slate-100 px-2 py-1 rounded">main</code>
            <ArrowRight className="h-4 w-4 text-slate-400" />
            <code className="bg-blue-100 text-blue-700 px-2 py-1 rounded">feature/neue-funktion</code>
            <ArrowRight className="h-4 w-4 text-slate-400" />
            <span className="text-slate-600">Entwicklung</span>
            <ArrowRight className="h-4 w-4 text-slate-400" />
            <span className="bg-purple-100 text-purple-700 px-2 py-1 rounded">Pull Request</span>
            <ArrowRight className="h-4 w-4 text-slate-400" />
            <span className="bg-green-100 text-green-700 px-2 py-1 rounded">Code Review</span>
            <ArrowRight className="h-4 w-4 text-slate-400" />
            <code className="bg-slate-100 px-2 py-1 rounded">main</code>
          </div>
        </div>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          <div className="bg-white rounded-lg p-4 border border-indigo-100">
            <h4 className="font-medium text-slate-900 mb-2">1. Feature Branch erstellen</h4>
            <code className="block text-xs bg-slate-800 text-green-400 p-2 rounded font-mono">
              git checkout -b feature/mein-feature
            </code>
          </div>
          <div className="bg-white rounded-lg p-4 border border-indigo-100">
            <h4 className="font-medium text-slate-900 mb-2">2. Änderungen committen</h4>
            <code className="block text-xs bg-slate-800 text-green-400 p-2 rounded font-mono">
              git commit -m &quot;feat: beschreibung&quot;
            </code>
          </div>
          <div className="bg-white rounded-lg p-4 border border-indigo-100">
            <h4 className="font-medium text-slate-900 mb-2">3. Branch pushen</h4>
            <code className="block text-xs bg-slate-800 text-green-400 p-2 rounded font-mono">
              git push -u origin feature/mein-feature
            </code>
          </div>
          <div className="bg-white rounded-lg p-4 border border-indigo-100">
            <h4 className="font-medium text-slate-900 mb-2">4. Pull Request in Gitea</h4>
            <code className="block text-xs bg-slate-800 text-green-400 p-2 rounded font-mono">
              http://macmini:3003 → Pull Request
            </code>
          </div>
        </div>

        <div className="mt-4 p-4 bg-indigo-100 rounded-lg">
          <h4 className="font-medium text-indigo-900 mb-2">Branch-Namenskonvention</h4>
          <div className="grid grid-cols-2 md:grid-cols-4 gap-2 text-sm">
            <div><code className="text-indigo-700">feature/</code> Neue Funktion</div>
            <div><code className="text-indigo-700">fix/</code> Bugfix</div>
            <div><code className="text-indigo-700">hotfix/</code> Dringender Fix</div>
            <div><code className="text-indigo-700">refactor/</code> Code-Umbau</div>
          </div>
        </div>
      </div>

      {/* Team Rules */}
      <div className="bg-amber-50 rounded-xl border border-amber-200 p-6">
        <h2 className="text-xl font-semibold text-amber-900 mb-4 flex items-center gap-2">
          <Users className="h-5 w-5 text-amber-600" />
          Team-Regeln
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
          <div className="flex items-start gap-3">
            <CheckCircle2 className="h-5 w-5 text-green-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Feature Branches nutzen</h3>
              <p className="text-sm text-slate-600">Nie direkt auf main pushen - immer über Pull Request</p>
            </div>
          </div>
          <div className="flex items-start gap-3">
            <CheckCircle2 className="h-5 w-5 text-green-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Code Review erforderlich</h3>
              <p className="text-sm text-slate-600">Mindestens 1 Approval vor dem Merge</p>
            </div>
          </div>
          <div className="flex items-start gap-3">
            <CheckCircle2 className="h-5 w-5 text-green-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Tests müssen grün sein</h3>
              <p className="text-sm text-slate-600">CI/CD Pipeline muss erfolgreich durchlaufen</p>
            </div>
          </div>
          <div className="flex items-start gap-3">
            <CheckCircle2 className="h-5 w-5 text-green-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Aussagekräftige Commits</h3>
              <p className="text-sm text-slate-600">Nutze Conventional Commits (feat:, fix:, etc.)</p>
            </div>
          </div>
          <div className="flex items-start gap-3">
            <CheckCircle2 className="h-5 w-5 text-green-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Branch aktuell halten</h3>
              <p className="text-sm text-slate-600">Regelmäßig main in deinen Branch mergen</p>
            </div>
          </div>
          <div className="flex items-start gap-3">
            <AlertTriangle className="h-5 w-5 text-amber-600 flex-shrink-0 mt-0.5" />
            <div>
              <h3 className="font-medium text-slate-900">Nie Force-Push auf main</h3>
              <p className="text-sm text-slate-600">Geschichte von main nie überschreiben</p>
            </div>
          </div>
        </div>
      </div>

      {/* Team Members Info */}
      <div className="bg-white rounded-xl border border-slate-200 p-6">
        <h2 className="text-xl font-semibold text-slate-900 mb-4 flex items-center gap-2">
          <Users className="h-5 w-5 text-indigo-600" />
          Team-Kommunikation
        </h2>

        <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
          <div className="bg-slate-50 rounded-lg p-4 text-center">
            <div className="text-3xl mb-2">💬</div>
            <h3 className="font-medium text-slate-900">Pull Request Kommentare</h3>
            <p className="text-sm text-slate-600 mt-1">Code-Diskussionen im PR</p>
          </div>
          <div className="bg-slate-50 rounded-lg p-4 text-center">
            <div className="text-3xl mb-2">📋</div>
            <h3 className="font-medium text-slate-900">Issues in Gitea</h3>
            <p className="text-sm text-slate-600 mt-1">Bugs & Features tracken</p>
          </div>
          <div className="bg-slate-50 rounded-lg p-4 text-center">
            <div className="text-3xl mb-2">🔔</div>
            <h3 className="font-medium text-slate-900">CI/CD Notifications</h3>
            <p className="text-sm text-slate-600 mt-1">Pipeline-Status per Mail</p>
          </div>
        </div>
      </div>
    </div>
  )
}
