/**
 * {{titleCase name}} Workflows
 *
 * Define your workflow logic here. Workflows are deterministic functions
 * that orchestrate activities and other workflows.
 *
 * Rules for workflows:
 * - Must be deterministic (same inputs = same outputs)
 * - Cannot use Date.now(), Math.random(), or other non-deterministic APIs
 * - Use sleep() instead of setTimeout()
 * - Use proxyActivities() to call activities
 */

import { log, proxyActivities, sleep } from "@temporalio/workflow";
import type * as activities from "./activities.js";

// Configure activity retries and timeouts
const { greet, processTask } = proxyActivities<typeof activities>({
  startToCloseTimeout: "30 seconds",
  retry: {
    maximumAttempts: 3,
  },
});

/**
 * Simple greeting workflow
 *
 * @param name - Name to greet
 * @returns Greeting message
 */
export async function greetingWorkflow(name: string): Promise<string> {
  return await greet(name);
}

/**
 * Task processing workflow with notification
 *
 * @param taskId - Task identifier
 * @param userId - User who owns the task
 * @returns Task status
 */
export async function taskWorkflow(
  taskId: string,
  userId: string
): Promise<{ status: string; taskId: string }> {
  log.info("Processing task", { taskId, userId });

  // Process the task
  const result = await processTask(taskId);

  // Wait before completing (demonstrates durable timers)
  await sleep("2 seconds");

  log.info("Task completed", { taskId, result });

  return {
    status: result,
    taskId,
  };
}
