import { redirect } from 'next/navigation';
import Link from 'next/link';
import { Plus, Calendar, Activity } from 'lucide-react';
import { getCurrentUser } from '@/lib/auth';
import { getPrograms, getTodaysSession } from '@/lib/db/programs';

export const dynamic = 'force-dynamic';

const DAY_LABELS = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

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

  const [programs, today] = await Promise.all([
    getPrograms(user.id),
    getTodaysSession(user.id),
  ]);

  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 justify-between">
          <h1 className="text-2xl sm:text-3xl font-bold text-white">Programs</h1>
          <Link
            href="/main/programs/new"
            className="inline-flex items-center gap-2 px-4 py-2 rounded bg-white text-black font-bold text-xs uppercase tracking-wider hover:bg-gray-100"
          >
            <Plus className="w-4 h-4" />
            New
          </Link>
        </div>
      </div>

      <div className="max-w-3xl mx-auto px-4 py-6 space-y-6">
        {today && today.day && (
          <section className="bg-emerald-950/30 border border-emerald-900 rounded p-5">
            <div className="flex items-start gap-3">
              <Calendar className="w-5 h-5 text-emerald-400 mt-0.5" />
              <div className="flex-1">
                <p className="text-[11px] uppercase tracking-wider text-emerald-400">
                  Today&apos;s session
                </p>
                <h2 className="text-lg font-bold text-white mt-1">
                  {today.day.name ?? `${DAY_LABELS[today.dayOfWeek]} session`}
                </h2>
                <p className="text-xs text-zinc-400 mt-0.5">
                  {today.program.name} · Week {today.weekNumber} ·{' '}
                  {today.day.exercises.length} exercise
                  {today.day.exercises.length === 1 ? '' : 's'}
                </p>
                <Link
                  href={`/main/programs/${today.program.id}`}
                  className="inline-block mt-3 text-xs text-emerald-300 underline"
                >
                  Open program →
                </Link>
              </div>
            </div>
          </section>
        )}

        {programs.length === 0 ? (
          <div className="text-center py-16">
            <Activity className="w-12 h-12 text-zinc-700 mx-auto mb-3" />
            <h2 className="text-lg font-semibold text-white mb-2">
              No programs yet
            </h2>
            <p className="text-zinc-500 mb-6 text-sm">
              Build a multi-week training plan, then follow it day by day.
            </p>
            <Link
              href="/main/programs/new"
              className="inline-flex items-center gap-2 px-5 py-2.5 rounded bg-white text-black font-semibold text-sm hover:bg-gray-100"
            >
              <Plus className="w-4 h-4" />
              Create your first program
            </Link>
          </div>
        ) : (
          <ul className="space-y-3">
            {programs.map((p) => (
              <li key={p.id}>
                <Link
                  href={`/main/programs/${p.id}`}
                  className="block bg-zinc-900 border border-zinc-800 rounded p-4 hover:border-zinc-700 transition"
                >
                  <div className="flex items-start justify-between gap-3">
                    <div className="min-w-0">
                      <h3 className="text-base font-semibold text-white truncate">
                        {p.name}
                      </h3>
                      <p className="text-xs text-zinc-500 mt-0.5">
                        {p.type} · {p.durationWeeks} week
                        {p.durationWeeks === 1 ? '' : 's'} · started{' '}
                        {new Date(p.startDate).toLocaleDateString()} ·{' '}
                        {p._count.weeks} week
                        {p._count.weeks === 1 ? '' : 's'} planned
                      </p>
                      {p.description && (
                        <p className="text-xs text-zinc-400 mt-1 line-clamp-2">
                          {p.description}
                        </p>
                      )}
                    </div>
                    {p.isActive && (
                      <span className="text-[10px] uppercase tracking-wider bg-emerald-900/50 text-emerald-300 px-2 py-0.5 rounded flex-shrink-0">
                        active
                      </span>
                    )}
                  </div>
                </Link>
              </li>
            ))}
          </ul>
        )}
      </div>
    </div>
  );
}
