"use client";

import { useEffect, useState } from "react";
import { Sparkles, ExternalLink } from "lucide-react";

export function AdVideoCard() {
  const [ad, setAd] = useState<any>(null);

  useEffect(() => {
    fetch("/api/ads?placement=native")
      .then(res => res.json())
      .then(data => {
        if (Array.isArray(data) && data.length > 0) {
          setAd(data[0]);
          fetch("/api/ads", {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify({ id: data[0].id, type: "impression" })
          }).catch(() => {});
        } else {
          fetch("/api/ads?placement=banner")
            .then(r => r.json())
            .then(bannerData => {
              if (Array.isArray(bannerData) && bannerData.length > 0) {
                setAd(bannerData[0]);
              }
            }).catch(() => {});
        }
      })
      .catch(() => {});
  }, []);

  if (!ad) return null;

  const handleClick = (e: React.MouseEvent) => {
    e.preventDefault();
    if (ad?.id) {
      fetch("/api/ads", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ id: ad.id, type: "click" })
      }).catch(() => {});
    }
    const targetUrl = ad?.linkUrl || "https://mschat.net";
    window.open(targetUrl, "_blank");
  };

  return (
    <div 
      onClick={handleClick}
      className="group block cursor-pointer select-none relative w-full h-full min-h-[220px]"
    >
      <div className="relative aspect-video w-full rounded-xl md:rounded-2xl overflow-hidden mb-2.5 p-4 bg-[#121218] border border-white/10 shadow-lg group-hover:border-amber-400/50 transition-all duration-300 flex flex-col justify-between">
        
        {ad.imageUrl && (
          <img src={ad.imageUrl} alt={ad.title} className="absolute inset-0 w-full h-full object-cover opacity-60 group-hover:opacity-75 transition-opacity" />
        )}

        {/* Top Badges */}
        <div className="flex items-center justify-between z-10">
          <div className="bg-black/70 backdrop-blur-md text-amber-300 text-[10px] font-black px-2.5 py-1 rounded-full uppercase tracking-wider flex items-center gap-1 border border-amber-300/30">
            <Sparkles size={11} className="text-amber-300" /> SPONSORISÉ
          </div>
          <div className="bg-emerald-500/20 border border-emerald-400/40 backdrop-blur-md text-emerald-300 text-[10px] font-extrabold px-2 py-0.5 rounded-full flex items-center gap-1.5">
            <span className="w-1.5 h-1.5 rounded-full bg-emerald-400 animate-ping" /> EN LIGNE
          </div>
        </div>

        {/* Middle Content */}
        <div className="z-10 my-auto">
          <h3 className="font-black text-sm md:text-base text-white tracking-wide uppercase line-clamp-1 group-hover:text-amber-300 transition-colors drop-shadow">
            {ad.title || "Publicité Officielle"}
          </h3>
          {ad.description && (
            <p className="text-xs text-white/90 font-medium line-clamp-2 leading-relaxed mt-1 drop-shadow">
              {ad.description}
            </p>
          )}
        </div>

        {/* Bottom CTA Button */}
        <div className="z-10 pt-2 flex items-center justify-between border-t border-white/15">
          <span className="text-[11px] text-white/70 font-semibold truncate max-w-[120px]">
            Sponsor
          </span>
          <span className="text-xs font-black bg-amber-400 text-black px-3 py-1 rounded-lg group-hover:bg-amber-300 transition-all flex items-center gap-1 shadow-md">
            VOIR L'OFFRE <ExternalLink size={12} />
          </span>
        </div>
      </div>
    </div>
  );
}
