import { redirect } from "next/navigation";
import Link from "next/link";
import { ChevronLeft } from "lucide-react";
import { getCurrentUser } from "@/lib/auth";
import { getExercises } from "@/lib/db/exercises";
import { getWorkoutById } from "@/lib/db/workouts";
import WorkoutForm, { EditWorkoutData } from "@/components/workouts/WorkoutForm";

export const metadata = {
  title: "Log Workout",
  description: "Log a new workout",
};

export default async function NewWorkoutPage({
  searchParams,
}: {
  searchParams: { edit?: string };
}) {
  const user = await getCurrentUser();
  if (!user) {
    redirect("/auth/login");
  }

  const exercises = await getExercises(user.id);

  // If ?edit=WORKOUT_ID, fetch existing workout for editing
  let editWorkout: EditWorkoutData | undefined;
  if (searchParams.edit) {
    const workout = await getWorkoutById(searchParams.edit);
    if (workout && workout.userId === user.id) {
      // Group sets by exercise
      const grouped: Record<string, EditWorkoutData["exercises"][number]> = {};
      for (const set of workout.setLogs) {
        const exId = set.exercise.id;
        let customMetrics: Record<string, string> | undefined;
        if ((set as any).customMetrics) {
          try {
            customMetrics = JSON.parse((set as any).customMetrics);
          } catch {
            customMetrics = undefined;
          }
        }
        if (!grouped[exId]) {
          grouped[exId] = {
            exercise: set.exercise,
            sets: [],
          };
        }
        grouped[exId].sets.push({
          setNumber: set.setNumber,
          reps: set.reps ?? undefined,
          weight: set.weight ?? undefined,
          rpe: set.rpe ?? undefined,
          durationSeconds: set.durationSeconds ?? undefined,
          distance: set.distance ?? undefined,
          calories: set.calories ?? undefined,
          customMetrics,
          notes: set.notes ?? undefined,
        });
      }

      editWorkout = {
        id: workout.id,
        name: workout.name || "",
        date: workout.date.toISOString(),
        durationMinutes: workout.durationMinutes,
        difficulty: workout.difficulty,
        caloriesBurned: (workout as any).caloriesBurned ?? null,
        notes: workout.notes,
        exercises: Object.values(grouped),
      };
    }
  }

  const isEditing = !!editWorkout;

  return (
    <div className="min-h-screen bg-[#0A0A0A] pb-24 md:pb-8">
      {/* Header */}
      <div className="border-b border-zinc-800 sticky top-0 z-40 bg-[#0A0A0A]">
        <div className="max-w-2xl mx-auto px-4 py-4 flex items-center gap-4">
          <Link
            href={isEditing ? `/main/workouts/${editWorkout!.id}` : "/main/workouts"}
            className="p-2 hover:bg-zinc-900 rounded-lg -ml-2 text-zinc-400 hover:text-white"
            aria-label="Back"
          >
            <ChevronLeft className="w-6 h-6" />
          </Link>
          <h1 className="text-2xl font-display text-white tracking-wider">
            {isEditing ? "Edit Workout" : "Log Workout"}
          </h1>
        </div>
      </div>

      {/* Form */}
      <div className="max-w-2xl mx-auto px-4 py-6 pb-12">
        <WorkoutForm
          exercises={exercises}
          recentlyUsedExercises={[]}
          editWorkout={editWorkout}
        />
      </div>
    </div>
  );
}
