// app/api/contact/route.ts
import { NextRequest, NextResponse } from "next/server";
import dbConnect from "@/lib/mongodb";
import Contact from "@/models/Contact";

export async function GET(request: NextRequest) {
  try {
    await dbConnect();

    const { searchParams } = new URL(request.url);
    const page = parseInt(searchParams.get("page") || "1");
    const limit = parseInt(searchParams.get("limit") || "10");
    const isRead = searchParams.get("isRead");
    const search = searchParams.get("search");
    const sort = searchParams.get("sort") || "createdAt";
    const order = searchParams.get("order") || "desc";

    let query: any = {};

    if (isRead !== null) {
      query.isRead = isRead === "true";
    }

    if (search) {
      query.$or = [
        { name: { $regex: search, $options: "i" } },
        { email: { $regex: search, $options: "i" } },
        { phone: { $regex: search, $options: "i" } },
        { message: { $regex: search, $options: "i" } },
      ];
    }

    const skip = (page - 1) * limit;

    // Create sort object
    const sortOptions: any = {};
    if (sort === "name") {
      sortOptions.name = order === "asc" ? 1 : -1;
    } else if (sort === "createdAt") {
      sortOptions.createdAt = order === "asc" ? 1 : -1;
    } else {
      sortOptions.createdAt = -1;
    }

    const contacts = await Contact.find(query)
      .sort(sortOptions)
      .skip(skip)
      .limit(limit);

    const total = await Contact.countDocuments(query);

    return NextResponse.json({
      success: true,
      data: contacts,
      pagination: {
        page,
        limit,
        total,
        pages: Math.ceil(total / limit),
      },
    });
  } catch (error: any) {
    console.error("Error fetching contacts:", error);
    return NextResponse.json(
      { success: false, error: error.message || "Failed to fetch contacts" },
      { status: 500 },
    );
  }
}

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

    const body = await request.json();
    const { name, email, phone, message } = body;

    // Validation
    if (!name || name.trim().length === 0) {
      return NextResponse.json(
        { success: false, error: "Name is required" },
        { status: 400 },
      );
    }

    if (!email || email.trim().length === 0) {
      return NextResponse.json(
        { success: false, error: "Email is required" },
        { status: 400 },
      );
    }

    // Email format validation
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailRegex.test(email)) {
      return NextResponse.json(
        { success: false, error: "Please enter a valid email address" },
        { status: 400 },
      );
    }

    if (!phone || phone.trim().length === 0) {
      return NextResponse.json(
        { success: false, error: "Phone number is required" },
        { status: 400 },
      );
    }

    // Phone number validation (10 digits)
    const phoneRegex = /^\d{10}$/;
    if (!phoneRegex.test(phone.replace(/\s/g, ""))) {
      return NextResponse.json(
        { success: false, error: "Please enter a valid 10-digit phone number" },
        { status: 400 },
      );
    }

    if (!message || message.trim().length === 0) {
      return NextResponse.json(
        { success: false, error: "Message is required" },
        { status: 400 },
      );
    }

    if (message.trim().length < 10) {
      return NextResponse.json(
        {
          success: false,
          error: "Message must be at least 10 characters long",
        },
        { status: 400 },
      );
    }

    const contact = await Contact.create({
      name: name.trim(),
      email: email.trim().toLowerCase(),
      phone: phone.trim(),
      message: message.trim(),
    });

    // Optional: Send email notification to admin
    // await sendEmailNotification(contact);

    return NextResponse.json(
      {
        success: true,
        data: contact,
        message: "Contact form submitted successfully",
      },
      { status: 201 },
    );
  } catch (error: any) {
    console.error("Error creating contact:", error);

    if (error.code === 11000) {
      return NextResponse.json(
        { success: false, error: "Duplicate entry detected" },
        { status: 400 },
      );
    }

    return NextResponse.json(
      {
        success: false,
        error: error.message || "Failed to submit contact form",
      },
      { status: 500 },
    );
  }
}

export async function PUT(request: NextRequest) {
  try {
    await dbConnect();

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

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

    const body = await request.json();
    const { name, email, phone, message, isRead } = body;

    const updateData: any = {};

    if (name !== undefined) updateData.name = name.trim();
    if (email !== undefined) updateData.email = email.trim().toLowerCase();
    if (phone !== undefined) updateData.phone = phone.trim();
    if (message !== undefined) updateData.message = message.trim();
    if (isRead !== undefined) updateData.isRead = isRead;

    // Validation if fields are provided
    if (updateData.name && updateData.name.length === 0) {
      return NextResponse.json(
        { success: false, error: "Name cannot be empty" },
        { status: 400 },
      );
    }

    if (updateData.email) {
      const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      if (!emailRegex.test(updateData.email)) {
        return NextResponse.json(
          { success: false, error: "Please enter a valid email address" },
          { status: 400 },
        );
      }
    }

    if (updateData.phone) {
      const phoneRegex = /^\d{10}$/;
      if (!phoneRegex.test(updateData.phone.replace(/\s/g, ""))) {
        return NextResponse.json(
          {
            success: false,
            error: "Please enter a valid 10-digit phone number",
          },
          { status: 400 },
        );
      }
    }

    if (updateData.message && updateData.message.length < 10) {
      return NextResponse.json(
        {
          success: false,
          error: "Message must be at least 10 characters long",
        },
        { status: 400 },
      );
    }

    const contact = await Contact.findByIdAndUpdate(id, updateData, {
      new: true,
      runValidators: true,
    });

    if (!contact) {
      return NextResponse.json(
        { success: false, error: "Contact not found" },
        { status: 404 },
      );
    }

    return NextResponse.json({
      success: true,
      data: contact,
      message: "Contact updated successfully",
    });
  } catch (error: any) {
    console.error("Error updating contact:", error);
    return NextResponse.json(
      { success: false, error: error.message || "Failed to update contact" },
      { status: 500 },
    );
  }
}

export async function DELETE(request: NextRequest) {
  try {
    await dbConnect();

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

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

    const contact = await Contact.findByIdAndDelete(id);

    if (!contact) {
      return NextResponse.json(
        { success: false, error: "Contact not found" },
        { status: 404 },
      );
    }

    return NextResponse.json({
      success: true,
      data: contact,
      message: "Contact deleted successfully",
    });
  } catch (error: any) {
    console.error("Error deleting contact:", error);
    return NextResponse.json(
      { success: false, error: error.message || "Failed to delete contact" },
      { status: 500 },
    );
  }
}

// PATCH endpoint for marking as read/unread
export async function PATCH(request: NextRequest) {
  try {
    await dbConnect();

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

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

    const body = await request.json();
    const { isRead } = body;

    if (isRead === undefined) {
      return NextResponse.json(
        { success: false, error: "isRead field is required" },
        { status: 400 },
      );
    }

    const contact = await Contact.findByIdAndUpdate(
      id,
      { isRead },
      { new: true },
    );

    if (!contact) {
      return NextResponse.json(
        { success: false, error: "Contact not found" },
        { status: 404 },
      );
    }

    return NextResponse.json({
      success: true,
      data: contact,
      message: `Contact marked as ${isRead ? "read" : "unread"}`,
    });
  } catch (error: any) {
    console.error("Error updating contact status:", error);
    return NextResponse.json(
      {
        success: false,
        error: error.message || "Failed to update contact status",
      },
      { status: 500 },
    );
  }
}
