'use client'

import React, { useState } from 'react'
import Link from 'next/link'
import {
  Shield, Code, Download, Smartphone, FileCode, Lock,
  ChevronRight, Copy, Check, Zap, Globe, Layers,
  BookOpen, Terminal
} from 'lucide-react'
import { SDKDocsSidebar } from '@/components/developers/SDKDocsSidebar'

type Framework = 'npm' | 'yarn' | 'pnpm'

const installCommands: Record<Framework, string> = {
  npm: 'npm install @breakpilot/consent-sdk',
  yarn: 'yarn add @breakpilot/consent-sdk',
  pnpm: 'pnpm add @breakpilot/consent-sdk',
}

function CopyButton({ text }: { text: string }) {
  const [copied, setCopied] = useState(false)

  const handleCopy = async () => {
    await navigator.clipboard.writeText(text)
    setCopied(true)
    setTimeout(() => setCopied(false), 2000)
  }

  return (
    <button
      onClick={handleCopy}
      className="p-2 hover:bg-gray-700 rounded transition-colors"
      title="Kopieren"
    >
      {copied ? (
        <Check className="w-4 h-4 text-green-400" />
      ) : (
        <Copy className="w-4 h-4 text-gray-400" />
      )}
    </button>
  )
}

export default function ConsentSDKHubPage() {
  const [selectedPM, setSelectedPM] = useState<Framework>('npm')

  const quickLinks = [
    {
      title: 'Installation',
      description: 'SDK in wenigen Minuten einrichten',
      href: '/developers/sdk/consent/installation',
      icon: Download,
      color: 'bg-blue-500',
    },
    {
      title: 'API Referenz',
      description: 'Vollstaendige API-Dokumentation',
      href: '/developers/sdk/consent/api-reference',
      icon: FileCode,
      color: 'bg-purple-500',
    },
    {
      title: 'Frameworks',
      description: 'React, Vue, Angular Integration',
      href: '/developers/sdk/consent/frameworks',
      icon: Layers,
      color: 'bg-green-500',
    },
    {
      title: 'Mobile SDKs',
      description: 'iOS, Android, Flutter',
      href: '/developers/sdk/consent/mobile',
      icon: Smartphone,
      color: 'bg-orange-500',
    },
    {
      title: 'Sicherheit',
      description: 'Best Practices & Compliance',
      href: '/developers/sdk/consent/security',
      icon: Lock,
      color: 'bg-red-500',
    },
  ]

  const features = [
    {
      title: 'DSGVO & TTDSG Konform',
      description: 'Vollstaendige Unterstuetzung fuer EU-Datenschutzverordnungen mit revisionssicherer Consent-Speicherung.',
      icon: Shield,
    },
    {
      title: 'Google Consent Mode v2',
      description: 'Native Integration mit automatischer Synchronisation zu Google Analytics und Ads.',
      icon: Globe,
    },
    {
      title: 'Script Blocking',
      description: 'Automatisches Blockieren von Third-Party Scripts bis zur Einwilligung.',
      icon: Code,
    },
    {
      title: 'Multi-Platform',
      description: 'Unterstuetzung fuer Web, PWA, iOS, Android und Flutter aus einer Codebasis.',
      icon: Smartphone,
    },
  ]

  return (
    <div className="min-h-screen bg-gray-50 flex">
      <SDKDocsSidebar />

      <main className="flex-1 ml-64">
        <div className="max-w-5xl mx-auto px-8 py-12">
          {/* Header */}
          <div className="mb-12">
            <div className="flex items-center gap-3 mb-4">
              <div className="w-12 h-12 rounded-xl bg-gradient-to-br from-violet-600 to-purple-600 flex items-center justify-center">
                <Shield className="w-7 h-7 text-white" />
              </div>
              <div>
                <h1 className="text-3xl font-bold text-gray-900">Consent SDK</h1>
                <div className="flex items-center gap-2 mt-1">
                  <span className="px-2 py-0.5 bg-green-100 text-green-800 text-xs font-medium rounded-full">
                    v1.0.0
                  </span>
                  <span className="text-sm text-gray-500">DSGVO/TTDSG Compliant</span>
                </div>
              </div>
            </div>
            <p className="text-lg text-gray-600 max-w-3xl">
              Das Consent SDK ermoeglicht DSGVO-konforme Einwilligungsverwaltung fuer Web, PWA und Mobile Apps.
              Mit nativer Unterstuetzung fuer React, Vue, Angular und Mobile Platforms.
            </p>
          </div>

          {/* Quick Install */}
          <div className="mb-12 bg-white rounded-xl border border-gray-200 overflow-hidden">
            <div className="px-6 py-4 border-b border-gray-200 flex items-center justify-between">
              <h2 className="font-semibold text-gray-900">Schnellinstallation</h2>
              <div className="flex gap-1 bg-gray-100 rounded-lg p-1">
                {(['npm', 'yarn', 'pnpm'] as const).map((pm) => (
                  <button
                    key={pm}
                    onClick={() => setSelectedPM(pm)}
                    className={`px-3 py-1 text-sm rounded-md transition-colors ${
                      selectedPM === pm
                        ? 'bg-white text-gray-900 shadow-sm'
                        : 'text-gray-600 hover:text-gray-900'
                    }`}
                  >
                    {pm}
                  </button>
                ))}
              </div>
            </div>
            <div className="bg-gray-900 px-6 py-4 flex items-center justify-between">
              <code className="text-green-400 font-mono text-sm">
                $ {installCommands[selectedPM]}
              </code>
              <CopyButton text={installCommands[selectedPM]} />
            </div>
          </div>

          {/* Quick Links */}
          <div className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Dokumentation</h2>
            <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
              {quickLinks.map((link) => (
                <Link
                  key={link.href}
                  href={link.href}
                  className="group p-4 bg-white rounded-xl border border-gray-200 hover:border-violet-300 hover:shadow-md transition-all"
                >
                  <div className="flex items-start gap-3">
                    <div className={`w-10 h-10 rounded-lg ${link.color} flex items-center justify-center shrink-0`}>
                      <link.icon className="w-5 h-5 text-white" />
                    </div>
                    <div className="flex-1 min-w-0">
                      <h3 className="font-medium text-gray-900 group-hover:text-violet-600 transition-colors flex items-center gap-1">
                        {link.title}
                        <ChevronRight className="w-4 h-4 opacity-0 group-hover:opacity-100 transition-opacity" />
                      </h3>
                      <p className="text-sm text-gray-500 mt-1">{link.description}</p>
                    </div>
                  </div>
                </Link>
              ))}
            </div>
          </div>

          {/* Quick Start Code */}
          <div className="mb-12 bg-white rounded-xl border border-gray-200 overflow-hidden">
            <div className="px-6 py-4 border-b border-gray-200">
              <h2 className="font-semibold text-gray-900">Schnellstart</h2>
            </div>
            <div className="bg-gray-900 p-6">
              <pre className="text-sm text-gray-300 font-mono overflow-x-auto">
{`import { ConsentManager } from '@breakpilot/consent-sdk';

// Manager initialisieren
const consent = new ConsentManager({
  apiEndpoint: 'https://api.example.com/consent',
  siteId: 'your-site-id',
});

// SDK starten
await consent.init();

// Consent pruefen
if (consent.hasConsent('analytics')) {
  // Analytics laden
}

// Events abonnieren
consent.on('change', (state) => {
  console.log('Consent geaendert:', state);
});`}
              </pre>
            </div>
          </div>

          {/* Features */}
          <div className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Features</h2>
            <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
              {features.map((feature) => (
                <div
                  key={feature.title}
                  className="p-4 bg-white rounded-xl border border-gray-200"
                >
                  <div className="flex items-start gap-3">
                    <div className="w-10 h-10 rounded-lg bg-violet-100 flex items-center justify-center shrink-0">
                      <feature.icon className="w-5 h-5 text-violet-600" />
                    </div>
                    <div>
                      <h3 className="font-medium text-gray-900">{feature.title}</h3>
                      <p className="text-sm text-gray-500 mt-1">{feature.description}</p>
                    </div>
                  </div>
                </div>
              ))}
            </div>
          </div>

          {/* Compliance Notice */}
          <div className="p-4 bg-blue-50 border border-blue-200 rounded-xl">
            <div className="flex items-start gap-3">
              <Shield className="w-5 h-5 text-blue-600 mt-0.5" />
              <div>
                <h3 className="font-medium text-blue-900">DSGVO & TTDSG Compliance</h3>
                <p className="text-sm text-blue-700 mt-1">
                  Das Consent SDK erfuellt alle Anforderungen der DSGVO (Art. 6, 7, 13, 14, 17, 20) und des TTDSG (§ 25).
                  Alle Einwilligungen werden revisionssicher gespeichert und koennen jederzeit exportiert werden.
                </p>
              </div>
            </div>
          </div>
        </div>
      </main>
    </div>
  )
}
