import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
import DashboardContent from '@/components/DashboardContent';
import DashboardSidebar from '@/components/DashboardSidebar';

export default async function DashboardPage() {
  const session = await getServerSession(authOptions);

  const isTestMode = process.env.TEST_MODE === 'true';
  const testEmailRecipient = process.env.TEST_EMAIL_RECIPIENT;
  const emailEnabled = process.env.EMAIL_ENABLED !== 'false';
  const autoWorkflows = process.env.AUTO_WORKFLOWS === 'true';

  const stats = {
    totalBookings: await prisma.booking.count(),
    reservedBookings: await prisma.booking.count({
      where: { status: 'RESERVED' },
    }),
    confirmedBookings: await prisma.booking.count({
      where: { status: 'CONFIRMED' },
    }),
    completedBookings: await prisma.booking.count({
      where: { status: 'COMPLETED' },
    }),
    totalLocations: await prisma.location.count(),
    totalPhotoboxes: await prisma.photobox.count({
      where: { active: true },
    }),
    totalDrivers: await prisma.user.count({
      where: { role: 'DRIVER', active: true },
    }),
  };

  const recentBookings = await prisma.booking.findMany({
    take: 5,
    orderBy: { createdAt: 'desc' },
    include: {
      location: true,
      photobox: true,
    },
  });

  return (
    <div className="min-h-screen bg-gradient-to-br from-gray-900 via-gray-800 to-gray-900">
      <div className="flex">
        <DashboardSidebar user={session?.user} />
        <main className="flex-1 p-8">
          {/* Development Mode Warning Banner */}
          {isTestMode && (
            <div className="mb-6 bg-yellow-500/10 border-2 border-yellow-500 rounded-xl p-4 shadow-lg">
              <div className="flex items-start gap-3">
                <div className="text-3xl">🧪</div>
                <div className="flex-1">
                  <h3 className="text-lg font-bold text-yellow-400 mb-1">
                    TEST-MODUS AKTIV
                  </h3>
                  <p className="text-yellow-200 text-sm mb-2">
                    Alle E-Mails werden an <strong>{testEmailRecipient || 'Test-E-Mail'}</strong> umgeleitet.
                    <br />
                    Echte Kunden erhalten KEINE E-Mails!
                  </p>
                  <div className="flex gap-4 text-xs text-yellow-300/80">
                    <span>📧 E-Mails: {emailEnabled ? '✅ Aktiv' : '❌ Deaktiviert'}</span>
                    <span>🤖 Auto-Workflows: {autoWorkflows ? '✅ Aktiv' : '❌ Deaktiviert'}</span>
                  </div>
                </div>
              </div>
            </div>
          )}

          {!emailEnabled && !isTestMode && (
            <div className="mb-6 bg-red-500/10 border-2 border-red-500 rounded-xl p-4 shadow-lg">
              <div className="flex items-start gap-3">
                <div className="text-3xl">⚠️</div>
                <div className="flex-1">
                  <h3 className="text-lg font-bold text-red-400 mb-1">
                    E-MAIL-VERSAND DEAKTIVIERT
                  </h3>
                  <p className="text-red-200 text-sm">
                    EMAIL_ENABLED=false - Kunden erhalten keine E-Mails!
                    <br />
                    Setzen Sie EMAIL_ENABLED="true" in der .env Datei.
                  </p>
                </div>
              </div>
            </div>
          )}

          <DashboardContent 
            user={session?.user} 
            stats={stats} 
            recentBookings={recentBookings}
          />
        </main>
      </div>
    </div>
  );
}
