'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 AngularIntegrationPage() {
  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-red-500 flex items-center justify-center">
              <span className="text-white font-bold">A</span>
            </div>
            <h1 className="text-3xl font-bold text-gray-900">Angular Integration</h1>
          </div>
          <p className="text-lg text-gray-600 mb-8">
            Service und Module fuer Angular 14+ 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>

          {/* Module Setup */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Module Setup</h2>
            <CodeBlock
              filename="app.module.ts"
              code={`import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ConsentModule } from '@breakpilot/consent-sdk/angular';
import { environment } from '../environments/environment';

@NgModule({
  imports: [
    BrowserModule,
    ConsentModule.forRoot({
      apiEndpoint: environment.consentApi,
      siteId: 'my-site',
      debug: !environment.production,
    }),
  ],
})
export class AppModule {}`}
            />
          </section>

          {/* Standalone Setup */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Standalone Setup (Angular 15+)</h2>
            <CodeBlock
              filename="app.config.ts"
              code={`import { ApplicationConfig } from '@angular/core';
import { provideConsent } from '@breakpilot/consent-sdk/angular';
import { environment } from '../environments/environment';

export const appConfig: ApplicationConfig = {
  providers: [
    provideConsent({
      apiEndpoint: environment.consentApi,
      siteId: 'my-site',
      debug: !environment.production,
    }),
  ],
};`}
            />
          </section>

          {/* Service Usage */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Service Usage</h2>
            <CodeBlock
              filename="components/analytics.component.ts"
              code={`import { Component, OnInit } from '@angular/core';
import { ConsentService } from '@breakpilot/consent-sdk/angular';

@Component({
  selector: 'app-analytics',
  template: \`
    <div *ngIf="hasAnalyticsConsent$ | async">
      <!-- Analytics Code hier -->
    </div>
  \`,
})
export class AnalyticsComponent implements OnInit {
  hasAnalyticsConsent$ = this.consentService.hasConsent$('analytics');

  constructor(private consentService: ConsentService) {}

  async loadAnalytics() {
    if (await this.consentService.hasConsent('analytics')) {
      // Load analytics
    }
  }
}`}
            />
          </section>

          {/* Cookie Banner */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Cookie Banner Component</h2>
            <CodeBlock
              filename="components/cookie-banner.component.ts"
              code={`import { Component } from '@angular/core';
import { ConsentService } from '@breakpilot/consent-sdk/angular';

@Component({
  selector: 'app-cookie-banner',
  template: \`
    <div
      *ngIf="isBannerVisible$ | async"
      class="fixed bottom-0 inset-x-0 bg-white border-t shadow-lg p-4 z-50"
    >
      <div class="max-w-4xl mx-auto flex items-center justify-between">
        <p class="text-sm text-gray-600">
          Wir verwenden Cookies um Ihr Erlebnis zu verbessern.
        </p>
        <div class="flex gap-2">
          <button (click)="rejectAll()" class="px-4 py-2 border rounded">
            Ablehnen
          </button>
          <button (click)="showSettings()" class="px-4 py-2 border rounded">
            Einstellungen
          </button>
          <button (click)="acceptAll()" class="px-4 py-2 bg-blue-600 text-white rounded">
            Alle akzeptieren
          </button>
        </div>
      </div>
    </div>
  \`,
})
export class CookieBannerComponent {
  isBannerVisible$ = this.consentService.isBannerVisible$;

  constructor(private consentService: ConsentService) {}

  async acceptAll() {
    await this.consentService.acceptAll();
  }

  async rejectAll() {
    await this.consentService.rejectAll();
  }

  showSettings() {
    this.consentService.showSettings();
  }
}`}
            />
          </section>

          {/* Directive */}
          <section className="mb-12">
            <h2 className="text-xl font-semibold text-gray-900 mb-4">ConsentGate Directive</h2>
            <CodeBlock
              filename="template.html"
              code={`<!-- Zeigt Element nur wenn Consent vorhanden -->
<iframe
  *consentGate="'social'"
  src="https://www.youtube.com/embed/VIDEO_ID"
  width="560"
  height="315"
></iframe>

<!-- Mit Custom Fallback -->
<div *consentGate="'analytics'; else noConsent">
  <app-analytics-dashboard></app-analytics-dashboard>
</div>
<ng-template #noConsent>
  <div class="bg-gray-100 p-4 rounded-lg text-center">
    <p>Bitte stimmen Sie Statistik-Cookies zu.</p>
    <button (click)="showSettings()">Einstellungen</button>
  </div>
</ng-template>`}
            />
          </section>

          {/* Service API */}
          <section>
            <h2 className="text-xl font-semibold text-gray-900 mb-4">Service 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/Method
                    </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">consent$</code></td>
                    <td className="px-6 py-4"><code className="text-gray-500">Observable&lt;ConsentState&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Observable des aktuellen Consent</td>
                  </tr>
                  <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">Observable&lt;boolean&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Reaktive Consent-Pruefung</td>
                  </tr>
                  <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">Promise&lt;boolean&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Async Consent-Pruefung</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">Observable&lt;boolean&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Banner-Sichtbarkeit</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">Promise&lt;void&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Akzeptiert alle</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">Promise&lt;void&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Lehnt alle ab</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">Promise&lt;void&gt;</code></td>
                    <td className="px-6 py-4 text-sm text-gray-600">Setzt spezifische Kategorien</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </section>
        </div>
      </main>
    </div>
  )
}
