/**
 * API Route: Cookie Banner Embed Code
 *
 * GET - Generiert den Embed-Code fuer den Cookie Banner
 */

import { NextRequest, NextResponse } from 'next/server'
import { CookieBannerConfig, CookieBannerEmbedCode } from '@/lib/sdk/einwilligungen/types'
import {
  generateCookieBannerConfig,
  generateEmbedCode,
} from '@/lib/sdk/einwilligungen/generator/cookie-banner'
import { PREDEFINED_DATA_POINTS } from '@/lib/sdk/einwilligungen/catalog/loader'

// In-Memory Storage (in Produktion mit configStorage aus config/route.ts teilen)
const configStorage = new Map<string, CookieBannerConfig>()

/**
 * GET /api/sdk/v1/einwilligungen/cookie-banner/embed-code
 *
 * Generiert den Embed-Code fuer den Cookie Banner
 *
 * Query Parameters:
 * - privacyPolicyUrl: string - URL zur Datenschutzerklaerung (default: /datenschutz)
 * - format: 'combined' | 'separate' - Ausgabeformat (default: combined)
 */
export async function GET(request: NextRequest) {
  try {
    const tenantId = request.headers.get('X-Tenant-ID')

    if (!tenantId) {
      return NextResponse.json(
        { error: 'Tenant ID required' },
        { status: 400 }
      )
    }

    const { searchParams } = new URL(request.url)
    const privacyPolicyUrl = searchParams.get('privacyPolicyUrl') || '/datenschutz'
    const format = searchParams.get('format') || 'combined'

    // Hole oder erstelle Konfiguration
    let config = configStorage.get(tenantId)

    if (!config) {
      config = generateCookieBannerConfig(tenantId, PREDEFINED_DATA_POINTS)
      configStorage.set(tenantId, config)
    }

    // Generiere Embed-Code
    const embedCode = generateEmbedCode(config, privacyPolicyUrl)

    if (format === 'separate') {
      // Separate Dateien zurueckgeben
      return NextResponse.json({
        html: embedCode.html,
        css: embedCode.css,
        js: embedCode.js,
        scriptTag: embedCode.scriptTag,
        instructions: {
          de: `
Fuegen Sie den folgenden Code in Ihre Website ein:

1. CSS in den <head>-Bereich:
   <style>${embedCode.css}</style>

2. HTML vor dem schliessenden </body>-Tag:
   ${embedCode.html}

3. JavaScript vor dem schliessenden </body>-Tag:
   <script>${embedCode.js}</script>

Alternativ koennen Sie die Dateien separat einbinden:
- /cookie-banner.css
- /cookie-banner.js
`,
          en: `
Add the following code to your website:

1. CSS in the <head> section:
   <style>${embedCode.css}</style>

2. HTML before the closing </body> tag:
   ${embedCode.html}

3. JavaScript before the closing </body> tag:
   <script>${embedCode.js}</script>

Alternatively, you can include the files separately:
- /cookie-banner.css
- /cookie-banner.js
`,
        },
      })
    }

    // Combined: Alles in einem HTML-Block
    const combinedCode = `
<!-- Cookie Banner - Start -->
<style>
${embedCode.css}
</style>

${embedCode.html}

<script>
${embedCode.js}
</script>
<!-- Cookie Banner - End -->
`.trim()

    return NextResponse.json({
      embedCode: combinedCode,
      scriptTag: embedCode.scriptTag,
      config: {
        tenantId: config.tenantId,
        categories: config.categories.map((c) => ({
          id: c.id,
          name: c.name,
          isRequired: c.isRequired,
          defaultEnabled: c.defaultEnabled,
        })),
        styling: config.styling,
      },
      instructions: {
        de: `Fuegen Sie den folgenden Code vor dem schliessenden </body>-Tag Ihrer Website ein.`,
        en: `Add the following code before the closing </body> tag of your website.`,
      },
    })
  } catch (error) {
    console.error('Error generating embed code:', error)
    return NextResponse.json(
      { error: 'Failed to generate embed code' },
      { status: 500 }
    )
  }
}

/**
 * POST /api/sdk/v1/einwilligungen/cookie-banner/embed-code
 *
 * Generiert Embed-Code mit benutzerdefinierten Optionen
 */
export async function POST(request: NextRequest) {
  try {
    const tenantId = request.headers.get('X-Tenant-ID')

    if (!tenantId) {
      return NextResponse.json(
        { error: 'Tenant ID required' },
        { status: 400 }
      )
    }

    const body = await request.json()
    const {
      privacyPolicyUrl = '/datenschutz',
      styling,
      texts,
      language = 'de',
    } = body

    // Hole oder erstelle Konfiguration
    let config = configStorage.get(tenantId)

    if (!config) {
      config = generateCookieBannerConfig(tenantId, PREDEFINED_DATA_POINTS, texts, styling)
    } else {
      // Wende temporaere Anpassungen an
      if (styling) {
        config = {
          ...config,
          styling: { ...config.styling, ...styling },
        }
      }
      if (texts) {
        config = {
          ...config,
          texts: { ...config.texts, ...texts },
        }
      }
    }

    const embedCode = generateEmbedCode(config, privacyPolicyUrl)

    // Generiere Preview HTML
    const previewHtml = `
<!DOCTYPE html>
<html lang="${language}">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cookie Banner Preview</title>
  <style>
    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
      background: #f1f5f9;
      min-height: 100vh;
      margin: 0;
      padding: 20px;
    }
    .preview-content {
      max-width: 800px;
      margin: 0 auto;
      padding: 40px;
      background: white;
      border-radius: 12px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    h1 { color: #1e293b; }
    p { color: #64748b; line-height: 1.6; }
    ${embedCode.css}
  </style>
</head>
<body>
  <div class="preview-content">
    <h1>Cookie Banner Preview</h1>
    <p>Dies ist eine Vorschau des Cookie Banners. In der produktiven Umgebung wird der Banner auf Ihrer Website angezeigt.</p>
  </div>

  ${embedCode.html}

  <script>
    ${embedCode.js}
    // Force show banner for preview
    setTimeout(() => {
      document.getElementById('cookieBanner')?.classList.add('active');
      document.getElementById('cookieBannerOverlay')?.classList.add('active');
    }, 100);
  </script>
</body>
</html>
`.trim()

    return NextResponse.json({
      embedCode: {
        html: embedCode.html,
        css: embedCode.css,
        js: embedCode.js,
        scriptTag: embedCode.scriptTag,
      },
      previewHtml,
      config: {
        tenantId: config.tenantId,
        categories: config.categories.length,
        styling: config.styling,
      },
    })
  } catch (error) {
    console.error('Error generating custom embed code:', error)
    return NextResponse.json(
      { error: 'Failed to generate embed code' },
      { status: 500 }
    )
  }
}
