import { createClient } from '@/lib/supabase/server' import { PrismaClient } from '@prisma/client' import { NextRequest, NextResponse } from 'next/server' const prisma = new PrismaClient() // POST /api/channels/follow - Follow/unfollow a channel export async function POST(req: NextRequest) { const supabase = await createClient() const { data: { user: supabaseUser }, error: authError } = await supabase.auth.getUser() if (authError || !supabaseUser) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } try { const body = await req.json() const { channelId, action } = body // action: 'follow' or 'unfollow' if (!channelId || !action) { return NextResponse.json( { error: 'Channel ID and action are required' }, { status: 400 } ) } // Get user const user = await prisma.user.findUnique({ where: { supabaseId: supabaseUser.id }, }) if (!user) { return NextResponse.json({ error: 'User not found' }, { status: 404 }) } if (action === 'follow') { // Create follower relationship const follower = await prisma.follower.create({ data: { userId: user.id, channelId, }, }) return NextResponse.json( { message: 'Followed successfully', follower }, { status: 201 } ) } else if (action === 'unfollow') { // Remove follower relationship await prisma.follower.delete({ where: { userId_channelId: { userId: user.id, channelId, }, }, }) return NextResponse.json( { message: 'Unfollowed successfully' }, { status: 200 } ) } else { return NextResponse.json( { error: 'Invalid action' }, { status: 400 } ) } } catch (error) { console.error('Error following/unfollowing channel:', error) return NextResponse.json( { error: 'Failed to follow/unfollow channel' }, { status: 500 } ) } }