"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import { Zap, Clock, X, ArrowRight } from "lucide-react";
import type { Profile } from "@/lib/constants";

interface Props {
  profile: Profile;
  isPremium: boolean;
}

export default function TrialConversionBanner({ profile, isPremium }: Props) {
  const [dismissed, setDismissed] = useState(true);

  useEffect(() => {
    if (!profile.id || !profile.premium_until) {
      setDismissed(true);
      return;
    }

    const key = `sinss_trial_dismissed_${profile.id}_${profile.premium_until}`;
    try {
      const isDismissed = sessionStorage.getItem(key);
      setDismissed(Boolean(isDismissed));
    } catch {
      setDismissed(false);
    }
  }, [profile.id, profile.premium_until]);

  const handleDismiss = () => {
    setDismissed(true);
    if (profile.id && profile.premium_until) {
      try {
        sessionStorage.setItem(`sinss_trial_dismissed_${profile.id}_${profile.premium_until}`, "1");
      } catch {}
    }
  };

  if (dismissed || !profile.premium_until) return null;

  const untilMs = Date.parse(profile.premium_until);
  if (isNaN(untilMs)) return null;

  const now = Date.now();
  const diffMs = untilMs - now;
  const isTrialActive = diffMs > 0;
  const daysLeft = Math.ceil(diffMs / (1000 * 60 * 60 * 24));

  // If user already has permanent premium via role or override, don't show expired banner
  if (!isTrialActive && isPremium) return null;

  // If expired more than 30 days ago, don't show
  const isRecentExpired = !isTrialActive && Math.abs(diffMs) < 30 * 24 * 60 * 60 * 1000;

  if (!isTrialActive && !isRecentExpired) return null;

  return (
    <div
      style={{
        display: "flex",
        alignItems: "center",
        justifyContent: "space-between",
        gap: 12,
        padding: "12px 16px",
        borderRadius: 12,
        background: isTrialActive ? "rgba(246, 230, 230, 0.08)" : "rgba(246, 219, 219, 0.08)",
        border: isTrialActive
          ? "1px solid rgba(246, 230, 230, 0.25)"
          : "1px solid rgba(246, 219, 219, 0.25)",
        color: "#fa2626",
        flexWrap: "wrap",
      }}
    >
      <div style={{ display: "flex", alignItems: "center", gap: 10, minWidth: 260, flex: 1 }}>
        <div
          style={{
            width: 32,
            height: 32,
            borderRadius: 8,
            background: isTrialActive ? "rgba(246, 230, 230, 0.15)" : "rgba(246, 219, 219, 0.15)",
            border: isTrialActive
              ? "1px solid rgba(246, 230, 230, 0.35)"
              : "1px solid rgba(246, 219, 219, 0.35)",
            display: "grid",
            placeItems: "center",
            color: isTrialActive ? "#faefef" : "#fae5e5",
            flexShrink: 0,
          }}
        >
          {isTrialActive ? <Clock size={16} /> : <Zap size={16} />}
        </div>
        <div>
          <strong style={{ display: "block", fontSize: 13, fontWeight: 700, color: "#000" }}>
            {isTrialActive
              ? `Premium Trial Active (${daysLeft} ${daysLeft === 1 ? "day" : "days"} left)`
              : "Your Premium Trial Has Ended"}
          </strong>
          <p style={{ margin: 0, fontSize: 11, color: "#551212", lineHeight: 1.4 }}>
            {isTrialActive
              ? "Keep your custom audio tracks, profile borders, and styling permanently with Lifetime Premium."
              : "Unlock permanent styling, audio capacity, and badges with Lifetime Premium for $6.99."}
          </p>
        </div>
      </div>

      <div style={{ display: "flex", alignItems: "center", gap: 8, flexShrink: 0 }}>
        <Link
          href="/premium"
          style={{
            display: "inline-flex",
            alignItems: "center",
            gap: 6,
            padding: "7px 13px",
            borderRadius: 8,
            background: isTrialActive ? "#f6e6e6" : "#f6dbdb",
            color: isTrialActive ? "#000" : "#000",
            fontSize: 12,
            fontWeight: 800,
            textDecoration: "none",
            transition: "transform 0.15s ease",
          }}
        >
          <span>{isTrialActive ? "Upgrade to Lifetime · $6.99" : "Get Lifetime · $6.99"}</span>
          <ArrowRight size={13} />
        </Link>
        <button
          type="button"
          onClick={handleDismiss}
          title="Dismiss notification"
          style={{
            width: 28,
            height: 28,
            borderRadius: 6,
            background: "transparent",
            border: "1px solid rgba(0, 0, 0, 0.1)",
            color: "#551212",
            display: "grid",
            placeItems: "center",
            cursor: "pointer",
          }}
        >
          <X size={14} />
        </button>
      </div>
    </div>
  );
}
