/**
 * Onboarding Banner - Dismissible banner for dashboard
 */

"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { Sparkles, X, ArrowRight } from "lucide-react";
import type { Lang } from "@/lib/config";
import type { OnboardingStatus } from "@/lib/types/onboarding.types";

interface OnboardingBannerProps {
  lang: Lang;
  status: OnboardingStatus;
  onDismiss?: () => void;
}

export default function OnboardingBanner({
  lang,
  status,
  onDismiss,
}: OnboardingBannerProps) {
  const router = useRouter();
  const isAr = lang === "ar";
  const [dismissing, setDismissing] = useState(false);
  const [visible, setVisible] = useState(true);

  const t = {
    title: isAr ? "أكمل إعداد حسابك" : "Complete Your Setup",
    description: isAr
      ? "قم بإنهاء الإعداد للبدء في أتمتة عيادتك"
      : "Finish setup to start automating your clinic",
    progress: isAr ? "{percent}% مكتمل" : "{percent}% Complete",
    completeBtn: isAr ? "إكمال الإعداد" : "Complete Setup",
    dismissBtn: isAr ? "تذكيري لاحقاً" : "Remind me later",
  };

  const handleDismiss = async () => {
    setDismissing(true);
    try {
      const response = await fetch("/api/onboarding/dismiss", {
        method: "POST",
      });

      if (response.ok) {
        setVisible(false);
        onDismiss?.();
      }
    } catch (error) {
      console.error("Error dismissing banner:", error);
    } finally {
      setDismissing(false);
    }
  };

  const handleCompleteSetup = () => {
    router.push(`/${lang}/dashboard/onboarding`);
  };

  if (!visible || status.isComplete || !status.shouldShowBanner) {
    return null;
  }

  return (
    <div
      className="relative overflow-hidden rounded-2xl bg-gradient-to-r from-brand-green via-emerald-600 to-teal-600 p-6 shadow-lg animate-slideDown"
      dir={isAr ? "rtl" : "ltr"}
    >
      {/* Decorative Elements */}
      <div className="absolute inset-0 bg-gradient-to-r from-white/10 to-transparent opacity-50" />
      <div className="absolute -right-10 -top-10 w-40 h-40 bg-white/10 rounded-full blur-2xl" />
      <div className="absolute -left-10 -bottom-10 w-40 h-40 bg-white/10 rounded-full blur-2xl" />

      {/* Content */}
      <div className="relative z-10 flex items-center justify-between gap-6">
        {/* Left: Icon + Text */}
        <div className="flex items-start gap-4 flex-1">
          <div className="flex-shrink-0 w-12 h-12 rounded-xl bg-white/20 backdrop-blur-sm flex items-center justify-center">
            <Sparkles className="w-6 h-6 text-white" />
          </div>
          <div className="flex-1">
            <h3 className="text-lg font-bold text-white mb-1">{t.title}</h3>
            <p className="text-sm text-white/90 mb-3">{t.description}</p>

            {/* Progress Bar */}
            <div className="flex items-center gap-3">
              <div className="flex-1 h-2 bg-white/20 rounded-full overflow-hidden max-w-xs">
                <div
                  className="h-full bg-white rounded-full transition-all duration-500"
                  style={{ width: `${status.progressPercentage}%` }}
                />
              </div>
              <span className="text-sm font-semibold text-white">
                {t.progress.replace(
                  "{percent}",
                  status.progressPercentage.toString(),
                )}
              </span>
            </div>
          </div>
        </div>

        {/* Right: Actions */}
        <div className="flex items-center gap-3">
          <button
            onClick={handleCompleteSetup}
            className="inline-flex items-center gap-2 px-6 py-3 rounded-xl bg-white text-brand-green font-semibold shadow-lg hover:shadow-xl hover:-translate-y-0.5 transition-all"
          >
            {t.completeBtn}
            <ArrowRight className="w-4 h-4" />
          </button>

          <button
            onClick={handleDismiss}
            disabled={dismissing}
            className="flex-shrink-0 p-2 rounded-lg text-white/80 hover:text-white hover:bg-white/10 transition-all disabled:opacity-50"
            title={t.dismissBtn}
          >
            <X className="w-5 h-5" />
          </button>
        </div>
      </div>

      <style jsx>{`
        @keyframes slideDown {
          from {
            opacity: 0;
            transform: translateY(-20px);
          }
          to {
            opacity: 1;
            transform: translateY(0);
          }
        }
        .animate-slideDown {
          animation: slideDown 0.5s ease-out forwards;
        }
      `}</style>
    </div>
  );
}
