import type { APIRoute } from 'astro'

export const prerender = false

const KIT_API_BASE = 'https://api.kit.com/v4'

export const POST: APIRoute = async ({ request }) => {
  const KIT_API_KEY = import.meta.env.KIT_API_SECRET_V4
  if (!KIT_API_KEY) {
    return new Response(JSON.stringify({ error: 'config_missing' }), {
      status: 500,
      headers: { 'Content-Type': 'application/json' },
    })
  }

  let body: { email: string; first_name?: string }
  try {
    body = await request.json()
  } catch {
    return new Response(JSON.stringify({ error: 'invalid_json' }), {
      status: 400,
      headers: { 'Content-Type': 'application/json' },
    })
  }

  const email = (body.email || '').trim().toLowerCase()
  if (!email || !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
    return new Response(JSON.stringify({ error: 'invalid_email' }), {
      status: 400,
      headers: { 'Content-Type': 'application/json' },
    })
  }

  try {
    const upstream = await fetch(`${KIT_API_BASE}/subscribers`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-Kit-Api-Key': KIT_API_KEY,
      },
      body: JSON.stringify({
        email_address: email,
        first_name: body.first_name || undefined,
        state: 'active',
      }),
      signal: AbortSignal.timeout(10000),
    })

    if (upstream.ok) {
      return new Response(JSON.stringify({ ok: true }), {
        status: 200,
        headers: { 'Content-Type': 'application/json' },
      })
    }
    if (upstream.status === 422) {
      return new Response(JSON.stringify({ ok: true, already: true }), {
        status: 200,
        headers: { 'Content-Type': 'application/json' },
      })
    }
    return new Response(
      JSON.stringify({ error: 'kit_failed', status: upstream.status }),
      { status: 502, headers: { 'Content-Type': 'application/json' } }
    )
  } catch (e) {
    return new Response(
      JSON.stringify({ error: 'upstream_failed', detail: (e as Error).message }),
      { status: 502, headers: { 'Content-Type': 'application/json' } }
    )
  }
}
