"use server"

import {
  changeOwnPassword,
  type PasswordChange,
} from "@/lib/services/change-password"
import { requireUser } from "@/lib/services/session"

/**
 * Replacing your own password.
 *
 * A thin adapter beside `language.ts`: it establishes who is asking and hands
 * the work to the service. Every tier reaches it, so `requireUser` and not
 * `requireAtLeast`. A password is the one thing in this product that belongs to
 * the person rather than to a rank.
 *
 * Nothing is revalidated. Every screen would render the same afterwards, and a
 * revalidation is a second place the three arguments below could end up.
 *
 * The answer is a reason and never a message, like `attemptSignIn`. What
 * somebody reads is chosen in the component from the dictionary, because a
 * server that returns English prose is a server that has decided which language
 * somebody reads.
 */
export async function changePassword(
  currentPassword: string,
  newPassword: string,
  again: string,
): Promise<PasswordChange> {
  const user = await requireUser()
  return changeOwnPassword(user.personId, currentPassword, newPassword, again)
}
