import { redirect } from 'next/navigation';
import Link from 'next/link';
import { ChevronLeft } from 'lucide-react';
import { getCurrentUser } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import ProgramEditor from '@/components/programs/ProgramEditor';

export const dynamic = 'force-dynamic';

export default async function NewProgramPage() {
  const user = await getCurrentUser();
  if (!user) redirect('/auth/login');

  const exercises = await prisma.exercise.findMany({
    where: { userId: user.id },
    select: { id: true, name: true, type: true },
    orderBy: [{ type: 'asc' }, { name: 'asc' }],
  });

  return (
    <div className="min-h-screen bg-[#0A0A0A]">
      <div className="border-b border-zinc-800">
        <div className="max-w-3xl mx-auto px-4 py-4 sm:py-6 flex items-center gap-3">
          <Link
            href="/main/programs"
            className="text-zinc-400 hover:text-white"
            aria-label="Back to programs"
          >
            <ChevronLeft className="w-5 h-5" />
          </Link>
          <h1 className="text-2xl sm:text-3xl font-bold text-white">
            New program
          </h1>
        </div>
      </div>

      <div className="max-w-3xl mx-auto px-4 py-6">
        <ProgramEditor exercises={exercises} />
      </div>
    </div>
  );
}
