"use server";

import { revalidatePath } from "next/cache";
import { prisma } from "./prisma";
import { requireCurrentUser } from "./session";

export async function sendFriendRequest(formData: FormData) {
  const user = await requireCurrentUser();
  const receiverId = String(formData.get("receiverId") || "");

  if (!receiverId || receiverId === user.id) return;

  const existing = await prisma.friendship.findFirst({
    where: {
      OR: [
        { requesterId: user.id, receiverId },
        { requesterId: receiverId, receiverId: user.id }
      ]
    },
    select: { id: true, status: true }
  });

  if (!existing) {
    await prisma.friendship.create({
      data: {
        requesterId: user.id,
        receiverId,
        status: "PENDING"
      }
    });
  } else if (existing.status === "DECLINED") {
    await prisma.friendship.update({
      where: { id: existing.id },
      data: {
        requesterId: user.id,
        receiverId,
        status: "PENDING"
      }
    });
  }

  revalidatePath("/friends");
  revalidatePath("/people");
  revalidatePath("/dashboard");
}

export async function acceptFriendRequest(formData: FormData) {
  await updateIncomingRequest(formData, "ACCEPTED");
}

export async function declineFriendRequest(formData: FormData) {
  await updateIncomingRequest(formData, "DECLINED");
}

export async function removeFriendship(formData: FormData) {
  const user = await requireCurrentUser();
  const friendshipId = String(formData.get("friendshipId") || "");
  if (!friendshipId) return;

  await prisma.friendship.deleteMany({
    where: {
      id: friendshipId,
      OR: [{ requesterId: user.id }, { receiverId: user.id }]
    }
  });

  revalidatePeople();
}

export async function cancelFriendRequest(formData: FormData) {
  const user = await requireCurrentUser();
  const friendshipId = String(formData.get("friendshipId") || "");
  if (!friendshipId) return;

  await prisma.friendship.deleteMany({
    where: {
      id: friendshipId,
      requesterId: user.id,
      status: "PENDING"
    }
  });

  revalidatePeople();
}

export async function blockUser(formData: FormData) {
  const user = await requireCurrentUser();
  const targetId = String(formData.get("targetId") || "");
  if (!targetId || targetId === user.id) return;

  const existing = await prisma.friendship.findFirst({
    where: {
      OR: [
        { requesterId: user.id, receiverId: targetId },
        { requesterId: targetId, receiverId: user.id }
      ]
    },
    select: { id: true }
  });

  if (existing) {
    await prisma.friendship.update({
      where: { id: existing.id },
      data: { requesterId: user.id, receiverId: targetId, status: "BLOCKED" }
    });
  } else {
    await prisma.friendship.create({
      data: { requesterId: user.id, receiverId: targetId, status: "BLOCKED" }
    });
  }

  revalidatePeople();
}

export async function unblockUser(formData: FormData) {
  const user = await requireCurrentUser();
  const friendshipId = String(formData.get("friendshipId") || "");
  if (!friendshipId) return;

  await prisma.friendship.deleteMany({
    where: {
      id: friendshipId,
      requesterId: user.id,
      status: "BLOCKED"
    }
  });

  revalidatePeople();
}

async function updateIncomingRequest(formData: FormData, status: "ACCEPTED" | "DECLINED") {
  const user = await requireCurrentUser();
  const friendshipId = String(formData.get("friendshipId") || "");
  if (!friendshipId) return;

  await prisma.friendship.updateMany({
    where: {
      id: friendshipId,
      receiverId: user.id,
      status: "PENDING"
    },
    data: { status }
  });

  revalidatePath("/friends");
  revalidatePath("/people");
  revalidatePath("/dashboard");
}

function revalidatePeople() {
  revalidatePath("/friends");
  revalidatePath("/people");
  revalidatePath("/dashboard");
}
