"use server"

import {
  approve,
  close,
  finish,
  remind,
  reportIssue,
  sendBack,
  start,
  verify,
  type CommandResult,
} from "@/lib/services/task-commands"
import { requireUser } from "@/lib/services/session"

/**
 * The eight buttons on `/tasks/[id]`, as server actions.
 *
 * Thin on purpose, and the thinness is the design: who is signed in is read
 * here from the session and never taken from the request, and everything else
 * happens in lib/services/task-commands.ts, which asks whether this person may
 * do this to this task before it writes a thing.
 *
 * `requireUser` and not `requireAtLeast`: this page is not guarded by tier.
 * `routes.json` (`single_task_detail`) says visible actions depend on the
 * viewer's relation to THIS task, so an admin and a machine operator reach the
 * same eight functions and are told apart by their relation to the row, not by
 * their rank. Anybody signed in may call these; what happens next is decided by
 * the relation, and by nothing that arrived in the call.
 *
 * There is no validation here either. A note that is only spaces, a task id
 * that names nothing and a verb the viewer may not perform all come back as a
 * `CommandResult` refusal from the same place, because a rule enforced in an
 * adapter is a rule the next adapter forgets.
 */

export async function startTask(id: string): Promise<CommandResult> {
  return start(await requireUser(), id)
}

export async function finishTask(
  id: string,
  note: string,
): Promise<CommandResult> {
  return finish(await requireUser(), id, note)
}

export async function reportTaskIssue(
  id: string,
  note: string,
): Promise<CommandResult> {
  return reportIssue(await requireUser(), id, note)
}

export async function approveTask(id: string): Promise<CommandResult> {
  return approve(await requireUser(), id)
}

export async function sendTaskBack(
  id: string,
  note: string,
): Promise<CommandResult> {
  return sendBack(await requireUser(), id, note)
}

export async function verifyTask(id: string): Promise<CommandResult> {
  return verify(await requireUser(), id)
}

export async function closeTask(id: string): Promise<CommandResult> {
  return close(await requireUser(), id)
}

export async function remindTask(
  id: string,
  note: string,
): Promise<CommandResult> {
  return remind(await requireUser(), id, note)
}
