"use server";

import { notFound, redirect } from "next/navigation";

// Simple in-memory counter for testing server actions
let likeCount = 0;

export async function incrementLikes(): Promise<number> {
  likeCount++;
  return likeCount;
}

export async function getLikes(): Promise<number> {
  return likeCount;
}

export async function addMessage(formData: FormData): Promise<string> {
  const message = formData.get("message") as string;
  return `Received: ${message}`;
}

/**
 * Server action that calls redirect() — should result in a redirect response.
 */
export async function redirectAction(): Promise<void> {
  redirect("/about");
}

/**
 * Server action that throws a regular error.
 */
export async function errorAction(): Promise<string> {
  throw new Error("Action failed intentionally");
}

/**
 * Server action that calls notFound() — should produce a 404 response
 * with the rejected actionResult propagated to the client error boundary.
 *
 * Regression coverage for #1340: Next.js sets statusCode = 404 when an
 * action throws via `notFound()`; vinext must match that contract.
 */
export async function notFoundAction(): Promise<void> {
  notFound();
}

/**
 * Server action that returns complex data (object, array, Date).
 */
export async function complexDataAction(): Promise<{
  items: string[];
  count: number;
  nested: { key: string };
}> {
  return {
    items: ["alpha", "beta", "gamma"],
    count: 3,
    nested: { key: "value" },
  };
}

/**
 * Server action for useActionState — accepts previous state and formData,
 * returns new state. This is the idiomatic Next.js 15+ pattern.
 */
export async function counterAction(
  prevState: { count: number },
  formData: FormData,
): Promise<{ count: number }> {
  const action = formData.get("action") as string;
  if (action === "increment") {
    return { count: prevState.count + 1 };
  }
  if (action === "decrement") {
    return { count: prevState.count - 1 };
  }
  return prevState;
}

/**
 * Server action for useActionState that calls redirect() on success.
 * This tests issue #589 — redirect() should not cause state to become undefined.
 */
export async function redirectWithActionState(
  _prevState: { success: boolean; error?: string },
  formData: FormData,
): Promise<{ success: boolean; error?: string }> {
  const shouldRedirect = formData.get("redirect") === "true";
  if (shouldRedirect) {
    redirect("/action-state-test");
  }
  return { success: true };
}
