import { auth } from '@/lib/auth';
import { redirect } from 'next/navigation';
import Link from 'next/link';
import { TeamCard } from '@/components/teams';
import { Button } from '@/components/ui';
import { getUserTeams } from '@/services/teams';

export default async function TeamsPage() {
  const session = await auth();

  if (!session?.user?.id) {
    redirect('/login');
  }

  const teams = await getUserTeams(session.user.id);

  return (
    <main className="mx-auto max-w-7xl px-4 py-8">
      {/* Header */}
      <div className="mb-8 flex flex-col sm:flex-row sm:items-center justify-between gap-4">
        <div>
          <h1 className="text-3xl font-bold text-foreground">Équipes</h1>
          <p className="mt-1 text-muted">
            {teams.length} équipe{teams.length !== 1 ? 's' : ''}
          </p>
        </div>
        <div>
          <Link href="/teams/new">
            <Button className="bg-[var(--purple)] text-white hover:opacity-90 border-transparent">
              Créer une équipe
            </Button>
          </Link>
        </div>
      </div>

      {/* Teams Grid */}
      {teams.length > 0 ? (
        <div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
          {teams.map((team: (typeof teams)[number]) => (
            <TeamCard key={team.id} team={team as Parameters<typeof TeamCard>[0]['team']} />
          ))}
        </div>
      ) : (
        <div className="flex flex-col items-center justify-center rounded-xl border border-border bg-card py-16">
          <div className="text-4xl">👥</div>
          <div className="mt-4 text-lg font-medium text-foreground">Aucune équipe</div>
          <div className="mt-1 text-sm text-muted">
            Créez votre première équipe pour commencer à définir des OKRs
          </div>
          <Link href="/teams/new" className="mt-6">
            <Button className="!bg-[var(--purple)] !text-white hover:!bg-[var(--purple)]/90">
              Créer une équipe
            </Button>
          </Link>
        </div>
      )}
    </main>
  );
}
