import ical from "ical-generator";
import { NextResponse } from "next/server";
import { prisma } from "../../../../lib/prisma";

export async function GET(
  _request: Request,
  context: { params: { token: string } }
) {
  const view = await prisma.userView.findUnique({
    where: { token: context.params.token },
    include: {
      items: { include: { event: true } },
      categories: true,
      exclusions: true,
      user: true
    }
  });

  if (!view) {
    return NextResponse.json({ error: "Not found" }, { status: 404 });
  }

  const calendar = ical({
    name: `Vereinskalender - ${view.name}`,
    timezone: "Europe/Berlin"
  });

  const excludedIds = new Set(view.exclusions.map((item) => item.eventId));
  const explicitEvents = view.items
    .map((item) => item.event)
    .filter((event) => event.status === "APPROVED");

  const categoryIds = view.categories.map((item) => item.categoryId);
  const categoryEvents =
    categoryIds.length > 0
      ? await prisma.event.findMany({
          where: { categoryId: { in: categoryIds }, status: "APPROVED" }
        })
      : [];

  const combined = [...explicitEvents, ...categoryEvents].filter(
    (event, index, all) =>
      all.findIndex((item) => item.id === event.id) === index &&
      !excludedIds.has(event.id)
  );

  combined.forEach((event) => {
      const start = event.startAt;
      const end =
        event.endAt || new Date(event.startAt.getTime() + 3 * 60 * 60 * 1000);
      calendar.createEvent({
        id: event.id,
        summary: event.title,
        description: event.description || undefined,
        location: event.location || undefined,
        start,
        end
      });
    });

  return new NextResponse(calendar.toString(), {
    headers: {
      "Content-Type": "text/calendar; charset=utf-8"
    }
  });
}
