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

import { getServerSession } from "next-auth";

export async function GET(
  req: Request,
  { params }: { params: { billboardId: string } }
) {
  try {

    if (!params.billboardId) {
      return new NextResponse("Panneau d'affichage requis", { status: 400 });
    }

    const billboard = await prismadb.billboard.findUnique({
      where: {
        id: params.billboardId,
      }
    });

    return NextResponse.json(billboard);
  } catch (error) {
    console.log('[BILLBOARD_GET]', error);
    return new NextResponse("Internal error", { status: 500 })
  }
};

export async function PATCH(
  req: Request,
  { params }: { params: { storeId: string, billboardId: string } }
) {
  try {
    const session = await getServerSession();
    const userId = session?.user?.email;

    const body = await req.json();

    const { name, title, description, imageUrl } = body;

    if (!userId) {
      return new NextResponse("Non authentifié", { status: 401 });
    }

    if (!name) {
      return new NextResponse("Nom requis", { status: 400 });
    }

    if (!title) {
      return new NextResponse("Titre requis", { status: 400 });
    }

    if (!description) {
      return new NextResponse("Description requise", { status: 400 });
    }

    if (!imageUrl) {
      return new NextResponse("Image requise", { status: 400 });
    }

    if (!params.billboardId) {
      return new NextResponse("Panneau d'affichage requis", { status: 400 });
    }

    const storeByUserId = await prismadb.store.findFirst({
      where: {
        id: params.storeId,
      },
      include: {
        userToStore: {
          where: {
            userId: userId
          }
        }
      }
    });

    if (!storeByUserId) {
      return new NextResponse("Non autorisé", { status: 403 });
    }

    const billboard = await prismadb.billboard.updateMany({
      where: {
        id: params.billboardId,
      },
      data: {
        name,
        title,
        description,
        imageUrl
      }
    });

    return NextResponse.json(billboard);
  } catch (error) {
    console.log('[BILLBOARD_PATCH]', error);
    return new NextResponse("Internal error", { status: 500 })
  }
};

export async function DELETE(
  req: Request,
  { params }: { params: { storeId: string, billboardId: string } }
) {
  try {
    const session = await getServerSession();
    const userId = session?.user?.email;

    if (!userId) {
      return new NextResponse("Non authentifié", { status: 401 });
    }

    if (!params.billboardId) {
      return new NextResponse("Panneau d'affichage requis", { status: 400 });
    }

    const storeByUserId = await prismadb.store.findFirst({
      where: {
        id: params.storeId,
      },
      include: {
        userToStore: {
          where: {
            userId: userId
          }
        }
      }
    });

    if (!storeByUserId) {
      return new NextResponse("Non autorisé", { status: 403 });
    }

    const billboard = await prismadb.billboard.deleteMany({
      where: {
        id: params.billboardId,
      }
    });

    return NextResponse.json(billboard);
  } catch (error) {
    console.log('[BILLBOARD_DELETE]', error);
    return new NextResponse("Internal error", { status: 500 })
  }
};