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

import { getServerSession } from "next-auth";

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

    if (!params.featureId) {
      return new NextResponse("Id de caractéristique requise", { status: 400 });
    }

    const feature = await prismadb.featureValue.findMany({
      where: {
        featureId: params.featureId,
      }
    });

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

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

    const body = await req.json();

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

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

    if (!params.storeId) {
      return new NextResponse("Id de boutique 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 featureValue = await prismadb.featureValue.create({
      data: {
        value,
        featureId : params.featureId,
        storeId: params.storeId
      }
    });

    return NextResponse.json(featureValue);

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

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

    const body = await req.json();

    const { name } = body;

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

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

    if (!params.featureId) {
      return new NextResponse("Id de caractéristique requise", { 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 feature = await prismadb.feature.updateMany({
      where: {
        id: params.featureId,
      },
      data: {
        name
      }
    });

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

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

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

    if (!params.featureId) {
      return new NextResponse("Id de caractéristique 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 feature = await prismadb.feature.deleteMany({
      where: {
        id: params.featureId,
      }
    });

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