import { prisma } from '@innungsapp/shared'
import { auth, getSanitizedHeaders } from '@/lib/auth'
import { headers } from 'next/headers'
import { redirect } from 'next/navigation'
import { StatsCards } from '@/components/stats/StatsCards'
import Link from 'next/link'
import { format } from 'date-fns'
import { de } from 'date-fns/locale'
import { NEWS_KATEGORIE_LABELS, TERMIN_TYP_LABELS } from '@innungsapp/shared'

export default async function DashboardPage() {
  const sanitizedHeaders = await getSanitizedHeaders()
  const session = await auth.api.getSession({ headers: sanitizedHeaders })
  if (!session?.user) redirect('/login')

  const userRole = await prisma.userRole.findFirst({
    where: { userId: session.user.id },
    include: { org: true },
  })
  if (!userRole) redirect('/login')

  const orgId = userRole.orgId
  const now = new Date()
  const weekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000)

  const [activeMembers, newsThisWeek, upcomingTermine, activeStellen, recentNews, nextTermine] =
    await Promise.all([
      prisma.member.count({ where: { orgId, status: 'aktiv' } }),
      prisma.news.count({ where: { orgId, publishedAt: { gte: weekAgo, not: null } } }),
      prisma.termin.count({ where: { orgId, datum: { gte: now } } }),
      prisma.stelle.count({ where: { orgId, aktiv: true } }),
      prisma.news.findMany({
        where: { orgId, publishedAt: { not: null } },
        orderBy: { publishedAt: 'desc' },
        take: 5,
        include: { author: { select: { name: true } } },
      }),
      prisma.termin.findMany({
        where: { orgId, datum: { gte: now } },
        orderBy: { datum: 'asc' },
        take: 3,
      }),
    ])

  return (
    <div className="space-y-8">
      <div>
        <h1 className="text-2xl font-bold text-gray-900">Übersicht</h1>
        <p className="text-gray-500 mt-1">{userRole.org.name}</p>
      </div>

      <StatsCards
        stats={[
          { label: 'Aktive Mitglieder', value: activeMembers, icon: '👥' },
          { label: 'News diese Woche', value: newsThisWeek, icon: '📰' },
          { label: 'Bevorstehende Termine', value: upcomingTermine, icon: '📅' },
          { label: 'Aktive Stellen', value: activeStellen, icon: '🎓' },
        ]}
      />

      <div className="grid grid-cols-1 lg:grid-cols-2 gap-6">
        {/* Recent News */}
        <div className="bg-white rounded-lg border p-6">
          <div className="flex items-center justify-between mb-4">
            <h2 className="font-semibold text-gray-900">Neueste Beiträge</h2>
            <Link href="/dashboard/news" className="text-sm text-brand-600 hover:underline">
              Alle anzeigen
            </Link>
          </div>
          <div className="space-y-3">
            {recentNews.map((n: typeof recentNews[number]) => (
              <div key={n.id} className="flex items-start gap-3 py-2 border-b last:border-0">
                <div className="flex-1 min-w-0">
                  <p className="font-medium text-sm text-gray-900 truncate">{n.title}</p>
                  <p className="text-xs text-gray-500 mt-0.5">
                    {n.publishedAt
                      ? format(n.publishedAt, 'dd. MMM yyyy', { locale: de })
                      : 'Entwurf'}{' '}
                    · {n.author?.name ?? 'Unbekannt'}
                  </p>
                </div>
                <span className="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded-full whitespace-nowrap">
                  {NEWS_KATEGORIE_LABELS[n.kategorie]}
                </span>
              </div>
            ))}
          </div>
        </div>

        {/* Upcoming Termine */}
        <div className="bg-white rounded-lg border p-6">
          <div className="flex items-center justify-between mb-4">
            <h2 className="font-semibold text-gray-900">Nächste Termine</h2>
            <Link href="/dashboard/termine" className="text-sm text-brand-600 hover:underline">
              Alle anzeigen
            </Link>
          </div>
          <div className="space-y-3">
            {nextTermine.length === 0 && (
              <p className="text-sm text-gray-500">Keine bevorstehenden Termine</p>
            )}
            {nextTermine.map((t: typeof nextTermine[number]) => (
              <div key={t.id} className="flex items-start gap-3 py-2 border-b last:border-0">
                <div className="text-center min-w-[40px]">
                  <p className="text-lg font-bold text-brand-500 leading-none">
                    {format(t.datum, 'dd', { locale: de })}
                  </p>
                  <p className="text-xs text-gray-500 uppercase">
                    {format(t.datum, 'MMM', { locale: de })}
                  </p>
                </div>
                <div className="flex-1 min-w-0">
                  <p className="font-medium text-sm text-gray-900 truncate">{t.titel}</p>
                  <p className="text-xs text-gray-500">{t.ort ?? 'Kein Ort angegeben'}</p>
                </div>
                <span className="text-xs bg-gray-100 text-gray-600 px-2 py-0.5 rounded-full whitespace-nowrap">
                  {TERMIN_TYP_LABELS[t.typ]}
                </span>
              </div>
            ))}
          </div>
        </div>
      </div>
    </div>
  )
}
