// controllers/Frontend/ReviewController.ts

import { NextRequest, NextResponse } from "next/server";
import mongoose from "mongoose";

import dbConnect from "@/lib/mongodb";

import Review from "@/models/Review";
import Product from "@/models/Product";


export async function POST(req: NextRequest) {
  try {
    await dbConnect();

    const body = await req.json();

    const {
      product,
      // customer,
      authorName,
      authorEmail,
      reviewTitle,
      reviewDescription,
      rating,
      images,
    } = body;

    // =========================
    // VALIDATION
    // =========================

    if (!product) {
      return NextResponse.json(
        {
          success: false,
          message: "Product is required",
        },
        { status: 400 }
      );
    }

    if (
      !mongoose.Types.ObjectId.isValid(
        product
      )
    ) {
      return NextResponse.json(
        {
          success: false,
          message: "Invalid product id",
        },
        { status: 400 }
      );
    }



    if (!authorName?.trim()) {
      return NextResponse.json(
        {
          success: false,
          message: "Name is required",
        },
        { status: 400 }
      );
    }

    if (!authorEmail?.trim()) {
      return NextResponse.json(
        {
          success: false,
          message: "Email is required",
        },
        { status: 400 }
      );
    }

    if (!reviewTitle?.trim()) {
      return NextResponse.json(
        {
          success: false,
          message: "Review title is required",
        },
        { status: 400 }
      );
    }

    if (!rating) {
      return NextResponse.json(
        {
          success: false,
          message: "Rating is required",
        },
        { status: 400 }
      );
    }

    // =========================
    // CHECK PRODUCT
    // =========================

    const productExists =
      await Product.findById(product);

    if (!productExists) {
      return NextResponse.json(
        {
          success: false,
          message: "Product not found",
        },
        { status: 404 }
      );
    }

    // =========================
    // CHECK USER
    // =========================

 

    // =========================
    // PREVENT DUPLICATE REVIEW
    // =========================

    // const alreadyReviewed =
    //   await Review.findOne({
    //     product,
    //   });

    // if (alreadyReviewed) {
    //   return NextResponse.json(
    //     {
    //       success: false,
    //       message:
    //         "You already reviewed this product",
    //     },
    //     { status: 400 }
    //   );
    // }

    // =========================
    // CREATE REVIEW
    // =========================

    const review = await Review.create({
      product,

      authorName:
        authorName.trim(),

      authorEmail:
        authorEmail.trim(),

      reviewTitle:
        reviewTitle.trim(),

      reviewDescription:
        reviewDescription?.trim() ||
        "",

      rating: Number(rating),

      images: images || [],

      status: "not-approved",
    });

    return NextResponse.json(
      {
        success: true,
        message:
          "Review submitted successfully",

        data: review,
      },
      {
        status: 201,
      }
    );
  } catch (error: any) {
    console.error(
      "Review Error:",
      error
    );

    return NextResponse.json(
      {
        success: false,
        message:
          error.message ||
          "Something went wrong",
      },
      {
        status: 500,
      }
    );
  }
}