import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/db'
import { getSession } from '@/lib/auth'
import { sendEmail } from '@/lib/email'

// POST: Send rapport link via email
export async function POST(req: NextRequest, { params }: { params: Promise<{ token: string }> }) {
  try {
    const { token } = await params
    const user = await getSession()
    if (!user) return NextResponse.json({ error: 'Nicht autorisiert' }, { status: 401 })

    const { email } = await req.json()
    if (!email) return NextResponse.json({ error: 'E-Mail-Adresse erforderlich' }, { status: 400 })

    const rapport = await (prisma as any).rapport.findUnique({
      where: { token },
      include: {
        tenant: { select: { name: true } },
        project: { select: { title: true, location: true } },
      },
    })

    if (!rapport) {
      return NextResponse.json({ error: 'Rapport nicht gefunden' }, { status: 404 })
    }

    const baseUrl = process.env.NEXTAUTH_URL || req.headers.get('origin') || `${req.headers.get('x-forwarded-proto') || 'https'}://${req.headers.get('host')}` || 'http://localhost:3000'
    const rapportUrl = `${baseUrl}/rapport/${rapport.token}`
    const pdfUrl = `${baseUrl}/api/rapports/${rapport.token}/pdf`

    const html = `
      <div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; max-width: 600px; margin: 0 auto;">
        <div style="background: #1a1a1a; color: white; padding: 20px 24px; border-radius: 8px 8px 0 0;">
          <h2 style="margin: 0; font-size: 18px;">Einsatzrapport</h2>
          <p style="margin: 4px 0 0; font-size: 13px; opacity: 0.8;">${rapport.tenant?.name || ''}</p>
        </div>
        <div style="border: 1px solid #e5e7eb; border-top: none; padding: 24px; border-radius: 0 0 8px 8px;">
          <table style="width: 100%; font-size: 14px; margin-bottom: 16px;">
            <tr><td style="color: #6b7280; padding: 4px 0;">Rapport-Nr.</td><td style="font-weight: 600;">${rapport.reportNumber}</td></tr>
            <tr><td style="color: #6b7280; padding: 4px 0;">Einsatz</td><td style="font-weight: 600;">${rapport.project?.title || '—'}</td></tr>
            <tr><td style="color: #6b7280; padding: 4px 0;">Standort</td><td>${rapport.project?.location || '—'}</td></tr>
            <tr><td style="color: #6b7280; padding: 4px 0;">Erstellt</td><td>${new Date(rapport.createdAt).toLocaleString('de-CH')}</td></tr>
          </table>
          <div style="margin: 20px 0;">
            <a href="${rapportUrl}" style="display: inline-block; background: #1a1a1a; color: white; padding: 10px 24px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 14px; margin-right: 8px;">
              Rapport ansehen
            </a>
            <a href="${pdfUrl}" style="display: inline-block; background: white; color: #1a1a1a; padding: 10px 24px; border-radius: 6px; text-decoration: none; font-weight: 600; font-size: 14px; border: 1px solid #d1d5db;">
              PDF herunterladen
            </a>
          </div>
          <p style="font-size: 12px; color: #9ca3af; margin-top: 16px;">
            Dieser Link ist öffentlich zugänglich — keine Anmeldung nötig.<br/>
            Gesendet von ${user.name || user.email} via app.lageplan.ch
          </p>
        </div>
      </div>
    `

    await sendEmail(
      email,
      `Einsatzrapport ${rapport.reportNumber} — ${rapport.project?.title || 'Lageplan'}`,
      html
    )

    return NextResponse.json({ success: true, message: `Rapport an ${email} gesendet` })
  } catch (error) {
    console.error('Error sending rapport email:', error)
    return NextResponse.json({ error: 'E-Mail konnte nicht gesendet werden' }, { status: 500 })
  }
}
