/**
 * Admin: Run EV Simulation for a Sports Season
 *
 * Action-only route. POSTing here:
 *   1. Delegates validation + execution to the shared simulator runner
 *   6. Redirects back to the sports season admin page
 */

import { redirect } from "react-router";
import type { Route } from "./+types/admin.sports-seasons.$id.simulate";
import { runSportsSeasonSimulation } from "~/services/simulations/runner";

export async function action({ params }: Route.ActionArgs) {
  const sportsSeasonId = params.id;
  try {
    await runSportsSeasonSimulation(sportsSeasonId);
  } catch (error) {
    throw new Response(error instanceof Error ? error.message : "Simulation failed", {
      status: error instanceof Error && error.message.includes("already running") ? 409 : 400,
    });
  }

  return redirect(`/admin/sports-seasons/${sportsSeasonId}/expected-values`);
}
