'use server';

import { UpdateUserStatusMutation } from '@/lib/mutations/admin/UpdateUserStatusMutation';
import { DeleteUserMutation } from '@/lib/mutations/admin/DeleteUserMutation';
import { revalidatePath } from 'next/cache';
import { Result } from '@/lib/contracts/Result';
import { routes } from '@/lib/routing/RouteConfig';

/**
 * Server actions for admin operations
 *
 * All write operations must enter through server actions.
 * Actions are thin wrappers that handle framework concerns (revalidation).
 * Business logic is handled by Mutations.
 * All actions return Result types for type-safe error handling.
 */

/**
 * Update user status
 *
 * @param userId - The ID of the user to update
 * @param status - The new status to set
 * @returns Result with success indicator or error
 */
export async function updateUserStatus(userId: string, status: string): Promise<Result<{ success: boolean }, string>> {
  const mutation = new UpdateUserStatusMutation();
  const result = await mutation.execute({ userId, status });
  
  if (result.isErr()) {
    console.error('updateUserStatus failed:', result.getError());
    return Result.err(result.getError());
  }
  
  revalidatePath(routes.admin.users);
  return Result.ok({ success: true });
}

/**
 * Delete user
 *
 * @param userId - The ID of the user to delete
 * @returns Result with success indicator or error
 */
export async function deleteUser(userId: string): Promise<Result<{ success: boolean }, string>> {
  const mutation = new DeleteUserMutation();
  const result = await mutation.execute({ userId });
  
  if (result.isErr()) {
    console.error('deleteUser failed:', result.getError());
    return Result.err(result.getError());
  }
  
  revalidatePath(routes.admin.users);
  return Result.ok({ success: true });
}