import { auth } from "@/auth";
import { NextRequest, NextResponse } from "next/server";
import { getLevelBridges, createLevelBridge, updateLevelBridge, deleteLevelBridge } from "@/lib/discord";

// GET - Fetch level bridges for a guild
export async function GET(
    request: NextRequest,
    { params }: { params: Promise<{ guildId: string }> }
) {
    const session = await auth();
    const { guildId } = await params;

    if (!session?.accessToken) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    try {
        const bridges = await getLevelBridges(guildId, session.accessToken as string);
        return NextResponse.json(bridges);
    } catch (error) {
        console.error("Error fetching bridges:", error);
        return NextResponse.json(
            { error: "Internal server error" },
            { status: 500 }
        );
    }
}

// POST - Create a new bridge
export async function POST(
    request: NextRequest,
    { params }: { params: Promise<{ guildId: string }> }
) {
    const session = await auth();
    const { guildId } = await params;

    if (!session?.accessToken) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    try {
        const body = await request.json();
        const result = await createLevelBridge(guildId, body, session.accessToken as string);

        if (!result.success) {
            return NextResponse.json(
                { error: result.error || "Failed to create bridge" },
                { status: 400, statusText: result.error } // Pass status text sometimes helps debugging
            );
        }

        return NextResponse.json({ success: true });
    } catch (error) {
        console.error("Error creating bridge:", error);
        return NextResponse.json(
            { error: "Internal server error" },
            { status: 500 }
        );
    }
}

// PUT - Update (or create) a bridge
export async function PUT(
    request: NextRequest,
    { params }: { params: Promise<{ guildId: string }> }
) {
    const session = await auth();
    const { guildId } = await params;

    if (!session?.accessToken) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    try {
        const body = await request.json();
        const result = await updateLevelBridge(guildId, body, session.accessToken as string);

        if (!result.success) {
            return NextResponse.json(
                { error: result.error || "Failed to update bridge" },
                { status: 400 }
            );
        }

        return NextResponse.json({ success: true });
    } catch (error) {
        console.error("Error updating bridge:", error);
        return NextResponse.json(
            { error: "Internal server error" },
            { status: 500 }
        );
    }
}

// DELETE - Delete a bridge
export async function DELETE(
    request: NextRequest,
    { params }: { params: Promise<{ guildId: string }> }
) {
    const session = await auth();
    const { guildId } = await params;

    if (!session?.accessToken) {
        return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    try {
        const body = await request.json();
        const result = await deleteLevelBridge(guildId, body.in_role_id, session.accessToken as string);

        if (!result.success) {
            return NextResponse.json(
                { error: result.error || "Failed to delete bridge" },
                { status: 400 }
            );
        }

        return NextResponse.json({ success: true });
    } catch (error) {
        console.error("Error deleting bridge:", error);
        return NextResponse.json(
            { error: "Internal server error" },
            { status: 500 }
        );
    }
}
