/**
 * File: route.ts
 * Created by: AI Assistant
 * Date: 2025-11-29
 * Purpose: Forum API endpoints for single forum operations
 * Part of: kreatiVortex - Platform Pembelajaran Tari Online
 */

import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';

export async function GET(
  _: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    const forum = await prisma.forum.findUnique({
      where: { id },
      include: {
        creator: {
          include: {
            user: {
              select: {
                name: true,
                image: true,
              },
            },
          },
        },
        posts: {
          include: {
            author: {
              include: {
                user: {
                  select: {
                    name: true,
                    image: true,
                  },
                },
              },
            },
            _count: {
              select: { comments: true },
            },
          },
          orderBy: {
            createdAt: 'desc',
          },
        },
      },
    });

    if (!forum) {
      return NextResponse.json(
        { success: false, message: 'Forum not found' },
        { status: 404 }
      );
    }

    return NextResponse.json({ success: true, data: forum });
  } catch (error) {
    console.error('Error fetching forum:', error);
    return NextResponse.json(
      { success: false, message: 'Failed to fetch forum' },
      { status: 500 }
    );
  }
}

export async function DELETE(
  _: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params;
    await prisma.forum.delete({
      where: { id },
    });

    return NextResponse.json({ success: true, message: 'Forum deleted' });
  } catch (error) {
    console.error('Error deleting forum:', error);
    return NextResponse.json(
      { success: false, message: 'Failed to delete forum' },
      { status: 500 }
    );
  }
}