"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import { X } from "lucide-react";
import { createClient } from "@/lib/supabase/client";

type Props = {
  profileUsername?: string;
  accent?: string;
};

export default function GuestSignupBanner({
  profileUsername,
}: Props) {
  const [visible, setVisible] = useState(false);
  const [exiting, setExiting] = useState(false);

  useEffect(() => {
    try {
      if (window.sessionStorage.getItem("sinss_guest_banner_dismissed") === "1") {
        return;
      }
    } catch {}

    const supabase = createClient();
    let isCancelled = false;

    supabase.auth.getSession().then(({ data: { session } }) => {
      if (isCancelled || session?.user) return;

      const timer = window.setTimeout(() => {
        if (!isCancelled) setVisible(true);
      }, 600);

      return () => window.clearTimeout(timer);
    });

    const {
      data: { subscription },
    } = supabase.auth.onAuthStateChange((_event, session) => {
      if (session?.user) {
        setVisible(false);
      }
    });

    return () => {
      isCancelled = true;
      subscription.unsubscribe();
    };
  }, []);

  const handleDismiss = () => {
    setExiting(true);
    try {
      window.sessionStorage.setItem("sinss_guest_banner_dismissed", "1");
    } catch {}
    window.setTimeout(() => {
      setVisible(false);
    }, 200);
  };

  if (!visible) return null;

  return (
    <>
      <aside
        aria-label="Create your page"
        className={`guestDockBanner ${exiting ? "guestDockExit" : "guestDockEnter"}`}
      >
        <div className="guestDockContent">
          <span className="guestDockDot" aria-hidden="true" />
          <span className="guestDockMessage">
            Create your own bio page on <strong className="guestDockBrand">sinss.lol</strong>
          </span>
        </div>

        <div className="guestDockActions">
          <Link
            href={`/claim${profileUsername ? `?ref=${encodeURIComponent(profileUsername)}` : ""}`}
            className="guestDockCta"
          >
            Claim handle
          </Link>

          <button
            type="button"
            onClick={handleDismiss}
            className="guestDockClose"
            aria-label="Dismiss banner"
            title="Dismiss"
          >
            <X size={13} />
          </button>
        </div>
      </aside>

      <style jsx global>{`
        .guestDockBanner {
          position: fixed;
          bottom: 20px;
          left: 50%;
          transform: translateX(-50%);
          z-index: 85;
          display: flex;
          align-items: center;
          justify-content: space-between;
          gap: 16px;
          padding: 8px 10px 8px 14px;
          border-radius: 8px;
          background: #0d1117;
          border: 1px solid #251818;
          box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
          color: #ad0606;
          font-family: inherit;
          max-width: calc(100vw - 32px);
          width: max-content;
          pointer-events: auto;
          user-select: none;
        }

        .guestDockEnter {
          animation: guestDockSlide 0.28s cubic-bezier(0.16, 1, 0.3, 1) forwards;
        }

        .guestDockExit {
          animation: guestDockFade 0.2s cubic-bezier(0.16, 1, 0.3, 1) forwards !important;
        }

        .guestDockContent {
          display: flex;
          align-items: center;
          gap: 8px;
          font-size: 13px;
          line-height: 1.4;
          white-space: nowrap;
        }

        .guestDockDot {
          width: 6px;
          height: 6px;
          border-radius: 50%;
          background: #ffffff;
          flex-shrink: 0;
        }

        .guestDockMessage {
          color: #510808;
          font-weight: 450;
        }

        .guestDockBrand {
          color: #fc5858;
          font-weight: 600;
        }

        .guestDockActions {
          display: flex;
          align-items: center;
          gap: 6px;
          flex-shrink: 0;
        }

        .guestDockCta {
          display: inline-flex;
          align-items: center;
          justify-content: center;
          padding: 4px 10px;
          font-size: 12px;
          font-weight: 600;
          color: #fc5858;
          background: #2d2121;
          border: 1px solid #201414;
          border-radius: 6px;
          text-decoration: none;
          transition: background 0.14s ease, border-color 0.14s ease, color 0.14s ease;
          white-space: nowrap;
        }

        .guestDockCta:hover {
          background: #251818;
          border-color: #240a0a;
          color: #000000;
        }

        .guestDockCta:active {
          background: #241919;
        }

        .guestDockClose {
          display: grid;
          place-items: center;
          width: 24px;
          height: 24px;
          padding: 0;
          border: none;
          background: transparent;
          color: #240a0a;
          border-radius: 4px;
          cursor: pointer;
          transition: color 0.14s ease, background 0.14s ease;
        }

        .guestDockClose:hover {
          color: #fc5858;
          background: #2d2121;
        }

        @keyframes guestDockSlide {
          from {
            opacity: 0;
            transform: translate(-50%, 16px);
          }
          to {
            opacity: 1;
            transform: translate(-50%, 0);
          }
        }

        @keyframes guestDockFade {
          from {
            opacity: 1;
            transform: translate(-50%, 0);
          }
          to {
            opacity: 0;
            transform: translate(-50%, 12px);
          }
        }

        @media (max-width: 640px) {
          .guestDockBanner {
            bottom: 14px;
            gap: 10px;
            padding: 6px 8px 6px 10px;
            border-radius: 7px;
          }
          .guestDockMessage {
            font-size: 12px;
          }
          .guestDockCta {
            padding: 3px 8px;
            font-size: 11.5px;
          }
        }
      `}</style>
    </>
  );
}
