import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from "@/lib/auth";
import { PrismaClient } from '@prisma/client';
import { notifyUser } from '@/lib/sse';

const prisma = new PrismaClient();

async function resolveUserId(email: string): Promise<string | null> {
    const user = await prisma.user.findUnique({
        where: { email },
        select: { id: true }
    });
    return user?.id ?? null;
}

export async function GET(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);

        if (!session?.user?.email) {
            return NextResponse.json(
                { error: 'Unauthorized' },
                { status: 401 }
            );
        }

        const userId = await resolveUserId(session.user.email);
        if (!userId) {
            return NextResponse.json({ error: 'User not found' }, { status: 404 });
        }

        const lists = await prisma.somedayList.findMany({
            where: { userId },
            orderBy: { order: 'asc' },
            include: {
                tasks: {
                    where: { deletedAt: null },
                    orderBy: { order: 'asc' }
                }
            }
        });

        return NextResponse.json({ lists });
    } catch (error) {
        console.error('Error fetching someday lists:', error);
        return NextResponse.json(
            { error: 'Failed to fetch someday lists' },
            { status: 500 }
        );
    }
}

export async function POST(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);

        if (!session?.user?.email) {
            return NextResponse.json(
                { error: 'Unauthorized' },
                { status: 401 }
            );
        }

        const userId = await resolveUserId(session.user.email);
        if (!userId) {
            return NextResponse.json({ error: 'User not found' }, { status: 404 });
        }

        const { title } = await request.json();

        if (!title) {
            return NextResponse.json(
                { error: 'Title is required' },
                { status: 400 }
            );
        }

        // Get max order
        const maxOrderList = await prisma.somedayList.findFirst({
            where: { userId },
            orderBy: { order: 'desc' }
        });
        const order = (maxOrderList?.order ?? -1) + 1;

        const list = await prisma.somedayList.create({
            data: {
                userId,
                title,
                order
            },
            include: { tasks: true } // Return with empty tasks array for frontend consistency
        });

        notifyUser(userId, "list-changed", { action: "created" });
        return NextResponse.json({ list });
    } catch (error) {
        console.error('Error creating someday list:', error);
        return NextResponse.json(
            { error: 'Failed to create someday list' },
            { status: 500 }
        );
    }
}

export async function DELETE(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);

        if (!session?.user?.email) {
            return NextResponse.json(
                { error: 'Unauthorized' },
                { status: 401 }
            );
        }

        const userId = await resolveUserId(session.user.email);
        if (!userId) {
            return NextResponse.json({ error: 'User not found' }, { status: 404 });
        }

        const { searchParams } = new URL(request.url);
        const id = searchParams.get('id');

        if (!id) {
            return NextResponse.json(
                { error: 'List ID is required' },
                { status: 400 }
            );
        }

        // Verify ownership
        const list = await prisma.somedayList.findUnique({
            where: { id }
        });

        if (!list || list.userId !== userId) {
            return NextResponse.json(
                { error: 'List not found or unauthorized' },
                { status: 404 }
            );
        }

        // Soft-delete tasks in this list (they can be recovered from trash)
        await prisma.task.updateMany({
            where: { somedayListId: id },
            data: { deletedAt: new Date(), somedayListId: null }
        });

        await prisma.somedayList.delete({
            where: { id }
        });

        notifyUser(userId, "list-changed", { action: "deleted" });
        return NextResponse.json({ success: true });
    } catch (error) {
        console.error('Error deleting someday list:', error);
        return NextResponse.json(
            { error: 'Failed to delete someday list' },
            { status: 500 }
        );
    }
}

export async function PATCH(request: NextRequest) {
    try {
        const session = await getServerSession(authOptions);

        if (!session?.user?.email) {
            return NextResponse.json(
                { error: 'Unauthorized' },
                { status: 401 }
            );
        }

        const userId = await resolveUserId(session.user.email);
        if (!userId) {
            return NextResponse.json({ error: 'User not found' }, { status: 404 });
        }

        const body = await request.json();

        // Handle Reordering (Array of { id, order })
        if (Array.isArray(body)) {
            const updates = body.map(async (item: { id: string; order: number }) => {
                return prisma.somedayList.updateMany({
                    where: {
                        id: item.id,
                        userId
                    },
                    data: { order: item.order }
                });
            });

            await Promise.all(updates);
            notifyUser(userId, "list-changed", { action: "reordered" });
            return NextResponse.json({ success: true });
        }

        // Handle Single Update (Title and/or Tab)
        const { id, title, tab } = body;

        if (!id) {
            return NextResponse.json(
                { error: 'ID is required' },
                { status: 400 }
            );
        }

        // Verify ownership
        const existingList = await prisma.somedayList.findUnique({
            where: { id }
        });

        if (!existingList || existingList.userId !== userId) {
            return NextResponse.json(
                { error: 'List not found or unauthorized' },
                { status: 404 }
            );
        }

        const data: Record<string, any> = {};
        if (title !== undefined) data.title = title;
        if (tab !== undefined) data.tab = tab;

        const list = await prisma.somedayList.update({
            where: { id },
            data
        });

        notifyUser(userId, "list-changed", { action: "updated" });
        return NextResponse.json({ list });
    } catch (error) {
        console.error('Error updating someday list:', error);
        return NextResponse.json(
            { error: 'Failed to update someday list' },
            { status: 500 }
        );
    }
}
