import { prisma } from "@/lib/prisma";
import Link from "next/link";
import { notFound } from "next/navigation";
import { Video, Eye, Crown, BadgeCheck } from "lucide-react";
import { MediaCard } from "@/components/MediaCard";

export default async function ChannelPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  
  const user = await prisma.user.findUnique({
    where: { id },
    include: {
      uploadedMedia: {
        where: { isPublished: true },
        orderBy: { createdAt: "desc" }
      }
    }
  });

  if (!user) return notFound();

  const totalViews = user.uploadedMedia.reduce((acc, curr) => acc + curr.views, 0);

  return (
    <div className="max-w-7xl mx-auto px-4 py-8">
      {/* Channel Header */}
      <div className="bg-[#1a1a1a] rounded-xl border border-[#333333] p-8 flex flex-col md:flex-row items-center gap-8 mb-12">
        <div className="w-32 h-32 rounded-full overflow-hidden border-4 border-[#e05a68] shrink-0">
          {user.image ? (
            <img src={user.image} alt={user.name || "User"} className="w-full h-full object-cover" />
          ) : (
            <div className="w-full h-full bg-[#12080b] flex items-center justify-center text-4xl font-black text-white">
              {user.name?.[0]?.toUpperCase() || "M"}
            </div>
          )}
        </div>
        
        <div className="flex-1 text-center md:text-left">
          <h1 className="text-4xl font-black mb-2 flex items-center justify-center md:justify-start gap-3">
            {user.name}
            {user.isVerified && <BadgeCheck size={28} className="text-[#e05a68]" />}
            {user.role !== "USER" && <Crown size={20} className="text-amber-400" />}
          </h1>
          <p className="text-[#aaaaaa] mb-6 max-w-2xl">
            Découvrez toutes les vidéos publiées par {user.name} sur MTBR.
          </p>
          
          <div className="flex flex-wrap items-center justify-center md:justify-start gap-4">
            <div className="bg-[#12080b] border border-[#2e1217] rounded-lg px-4 py-2 flex items-center gap-2">
              <Video size={16} className="text-[#e05a68]" />
              <span className="font-bold">{user.uploadedMedia.length}</span>
              <span className="text-[#aaaaaa] text-sm">Vidéos</span>
            </div>
            <div className="bg-[#12080b] border border-[#2e1217] rounded-lg px-4 py-2 flex items-center gap-2">
              <Eye size={16} className="text-[#e05a68]" />
              <span className="font-bold">{totalViews}</span>
              <span className="text-[#aaaaaa] text-sm">Vues totales</span>
            </div>
          </div>
        </div>
      </div>

      {/* Channel Videos */}
      <div className="border-b border-[#2e1217] mb-8 pb-4 flex items-center gap-6">
        <div className="font-bold text-[#e05a68] border-b-2 border-[#e05a68] pb-4 -mb-[18px]">
          VIDÉOS ({user.uploadedMedia.length})
        </div>
      </div>

      {user.uploadedMedia.length === 0 ? (
        <div className="text-center py-20 text-[#aaaaaa]">
          <Video size={48} className="mx-auto mb-4 opacity-20" />
          <p className="text-lg">Cet utilisateur n'a pas encore publié de vidéo.</p>
        </div>
      ) : (
        <div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-4">
          {user.uploadedMedia.map((media) => (
            <MediaCard key={media.id} media={media} />
          ))}
        </div>
      )}
    </div>
  );
}
