import { NextResponse } from "next/server"
import { sql } from "@/lib/db"

export async function POST(request: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: artistId } = await params
    const body = await request.json()
    const { userId } = body

    if (!userId) {
      return NextResponse.json({ error: "User ID required" }, { status: 400 })
    }

    const existing = await sql`
      SELECT id FROM artist_followers 
      WHERE artist_id = ${artistId} AND user_id = ${userId}
    `

    if (existing.length > 0) {
      return NextResponse.json({ success: true, following: true, message: "Already following" })
    }

    await sql`
      INSERT INTO artist_followers (id, artist_id, user_id, created_at)
      VALUES (gen_random_uuid(), ${artistId}, ${userId}, NOW())
    `

    // Update artist follower count
    await sql`
      UPDATE artists 
      SET total_followers = COALESCE(total_followers, 0) + 1
      WHERE id = ${artistId}
    `

    return NextResponse.json({ success: true, following: true })
  } catch (error) {
    console.error("Follow artist error:", error)
    return NextResponse.json({ error: "Failed to follow artist" }, { status: 500 })
  }
}

export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: artistId } = await params
    const { searchParams } = new URL(request.url)
    const userId = searchParams.get("userId")

    if (!userId) {
      return NextResponse.json({ error: "User ID required" }, { status: 400 })
    }

    const existing = await sql`
      SELECT id FROM artist_followers 
      WHERE artist_id = ${artistId} AND user_id = ${userId}
    `

    if (existing.length === 0) {
      return NextResponse.json({ success: true, following: false, message: "Not following" })
    }

    await sql`
      DELETE FROM artist_followers
      WHERE artist_id = ${artistId} AND user_id = ${userId}
    `

    // Update artist follower count
    await sql`
      UPDATE artists 
      SET total_followers = GREATEST(COALESCE(total_followers, 0) - 1, 0)
      WHERE id = ${artistId}
    `

    return NextResponse.json({ success: true, following: false })
  } catch (error) {
    console.error("Unfollow artist error:", error)
    return NextResponse.json({ error: "Failed to unfollow artist" }, { status: 500 })
  }
}

export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: artistId } = await params
    const { searchParams } = new URL(request.url)
    const userId = searchParams.get("userId")

    if (!userId) {
      return NextResponse.json({ error: "User ID required" }, { status: 400 })
    }

    const result = await sql`
      SELECT id FROM artist_followers 
      WHERE artist_id = ${artistId} AND user_id = ${userId}
    `

    return NextResponse.json({
      success: true,
      following: result.length > 0,
    })
  } catch (error) {
    console.error("Check follow status error:", error)
    return NextResponse.json({ error: "Failed to check follow status" }, { status: 500 })
  }
}
