import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { newsletterSubscribers } from '@/shared/schema';
import { eq, and } from 'drizzle-orm';

const SITE_ID = 'ten-sura-game.com';

export async function DELETE(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const numericId = parseInt(id, 10);

    if (isNaN(numericId)) {
      return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
    }

    await db
      .delete(newsletterSubscribers)
      .where(
        and(
          eq(newsletterSubscribers.id, numericId),
          eq(newsletterSubscribers.siteId, SITE_ID)
        )
      );

    return NextResponse.json({ success: true });
  } catch (error: any) {
    console.error('Error deleting subscriber:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}
