'use client'

import React, { useState } from 'react'
import { SDKDocsSidebar } from '@/components/SDKDocsSidebar'
import { Copy, Check } from 'lucide-react'

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">
      {copied ? <Check className="w-4 h-4 text-green-400" /> : <Copy className="w-4 h-4 text-gray-400" />}
    </button>
  )
}

function CodeBlock({ code, filename }: { code: string; filename?: string }) {
  return (
    <div className="bg-gray-900 rounded-lg overflow-hidden">
      {filename && (
        <div className="px-4 py-2 bg-gray-800 text-gray-400 text-xs border-b border-gray-700">
          {filename}
        </div>
      )}
      <div className="relative">
        <div className="absolute top-2 right-2">
          <CopyButton text={code} />
        </div>
        <pre className="p-4 text-sm text-gray-300 font-mono overflow-x-auto">
          <code>{code}</code>
        </pre>
      </div>
    </div>
  )
}

export default function ReactIntegrationPage() {
  return (
    <div className="min-h-screen bg-gray-50 flex">
      <SDKDocsSidebar />

      <main className="flex-1 ml-64">
        <div className="max-w-4xl mx-auto px-8 py-12">
          <div className="flex items-center gap-3 mb-2">
            <div className="w-10 h-10 rounded-lg bg-cyan-500 flex items-center justify-center">
              <span className="text-white font-bold">R</span>
            </div>
            <h1 className="text-3xl font-bold text-gray-900">React Integration</h1>
          </div>
          <p className="text-lg text-gray-600 mb-8">
            Hooks und Provider fuer React 17+ und Next.js Projekte.
          </p>

          {/* Installation */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Installation</h2>
            <CodeBlock code="npm install @breakpilot/consent-sdk" />
          </section>

          {/* Provider Setup */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Provider Setup</h2>
            <p className="text-gray-600 mb-4">
              Umschliessen Sie Ihre App mit dem ConsentProvider:
            </p>
            <CodeBlock
              filename="app/layout.tsx"
              code={`import { ConsentProvider } from '@breakpilot/consent-sdk/react';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="de">
      <body>
        <ConsentProvider
          config={{
            apiEndpoint: process.env.NEXT_PUBLIC_CONSENT_API!,
            siteId: 'my-site',
            debug: process.env.NODE_ENV === 'development',
          }}
        >
          {children}
        </ConsentProvider>
      </body>
    </html>
  );
}`}
            />
          </section>

          {/* useConsent Hook */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">useConsent Hook</h2>
            <p className="text-gray-600 mb-4">
              Verwenden Sie den Hook in jeder Komponente:
            </p>
            <CodeBlock
              filename="components/Analytics.tsx"
              code={`import { useConsent } from '@breakpilot/consent-sdk/react';

export function Analytics() {
  const { hasConsent, acceptAll, rejectAll, showSettings } = useConsent();

  if (!hasConsent('analytics')) {
    return null;
  }

  return (
    <Script
      src="https://www.googletagmanager.com/gtag/js?id=GA_ID"
      strategy="afterInteractive"
    />
  );
}`}
            />
          </section>

          {/* ConsentGate */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">ConsentGate Component</h2>
            <p className="text-gray-600 mb-4">
              Zeigt Inhalte nur wenn Consent vorhanden ist:
            </p>
            <CodeBlock
              filename="components/YouTubeEmbed.tsx"
              code={`import { ConsentGate } from '@breakpilot/consent-sdk/react';

export function YouTubeEmbed({ videoId }: { videoId: string }) {
  return (
    <ConsentGate
      category="social"
      fallback={
        <div className="bg-gray-100 p-4 rounded-lg text-center">
          <p>Video erfordert Ihre Zustimmung.</p>
          <button onClick={() => showSettings()}>
            Einstellungen oeffnen
          </button>
        </div>
      }
    >
      <iframe
        src={\`https://www.youtube.com/embed/\${videoId}\`}
        width="560"
        height="315"
        allowFullScreen
      />
    </ConsentGate>
  );
}`}
            />
          </section>

          {/* Custom Banner */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Custom Cookie Banner</h2>
            <CodeBlock
              filename="components/CookieBanner.tsx"
              code={`import { useConsent } from '@breakpilot/consent-sdk/react';

export function CookieBanner() {
  const {
    isBannerVisible,
    acceptAll,
    rejectAll,
    showSettings,
  } = useConsent();

  if (!isBannerVisible) return null;

  return (
    <div className="fixed bottom-0 inset-x-0 bg-white border-t shadow-lg p-4">
      <div className="max-w-4xl mx-auto flex items-center justify-between">
        <p className="text-sm text-gray-600">
          Wir verwenden Cookies um Ihr Erlebnis zu verbessern.
        </p>
        <div className="flex gap-2">
          <button
            onClick={rejectAll}
            className="px-4 py-2 text-sm border rounded"
          >
            Ablehnen
          </button>
          <button
            onClick={showSettings}
            className="px-4 py-2 text-sm border rounded"
          >
            Einstellungen
          </button>
          <button
            onClick={acceptAll}
            className="px-4 py-2 text-sm bg-blue-600 text-white rounded"
          >
            Alle akzeptieren
          </button>
        </div>
      </div>
    </div>
  );
}`}
            />
          </section>

          {/* Hook API */}
          <section>
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Hook API</h2>
            <div className="bg-white rounded-xl border border-gray-200 overflow-hidden">
              <table className="min-w-full divide-y divide-gray-200">
                <thead className="bg-gray-50">
                  <tr>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                      Property
                    </th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                      Typ
                    </th>
                    <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                      Beschreibung
                    </th>
                  </tr>
                </thead>
                <tbody className="divide-y divide-gray-200">
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">hasConsent</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">(category) =&gt; boolean</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Prueft Consent fuer Kategorie</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">consent</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">ConsentState | null</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Aktueller Consent-Status</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">acceptAll</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">() =&gt; Promise</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Akzeptiert alle Kategorien</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">rejectAll</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">() =&gt; Promise</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Lehnt alle ab (ausser essential)</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">setConsent</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">(input) =&gt; Promise</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Setzt spezifische Kategorien</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">isBannerVisible</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">boolean</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Banner sichtbar?</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">showBanner</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">() =&gt; void</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Zeigt den Banner</td>
                  </tr>
                  <tr>
                    <td className="px-6 py-4"><code className="text-violet-600">showSettings</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">() =&gt; void</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Oeffnet Einstellungen</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </section>
        </div>
      </main>
    </div>
  )
}
