"use client";

import React, { useState, useEffect } from "react";
import { formatPrice } from "@/utils/currencyHelper";

export default function ProductPricing({
  mrp,
  dealerPrice,
  currency = "INR",
}: {
  mrp: number | null | undefined;
  dealerPrice: number;
  currency?: string;
}) {
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [mounted, setMounted] = useState(false);

  useEffect(() => {
    setMounted(true);
    setIsLoggedIn(!!localStorage.getItem("access_token"));
  }, []);

  if (!mounted) {
    // Initial SSR render: placeholder
    return (
      <div className="flex flex-col sm:flex-row sm:items-end gap-4 sm:gap-8 mb-10 pb-10 border-b border-gray-100 min-h-[5rem]">
        <div className="h-10 w-48 bg-gray-100 animate-pulse rounded-lg"></div>
      </div>
    );
  }

  return (
    <div className="flex flex-col sm:flex-row sm:items-end gap-4 sm:gap-8 mb-10 pb-10 border-b border-gray-100 min-h-[5rem]">
      {isLoggedIn ? (
        <>
          <div className="flex flex-col">
            <span className="text-[13px] font-extrabold text-gray-400 mb-1 uppercase tracking-wider">Dealer Price</span>
            <span className="text-5xl font-black text-foreground tracking-tight">{formatPrice(dealerPrice, currency)}</span>
          </div>
          {mrp !== null && mrp !== undefined && mrp > dealerPrice && (
            <div className="flex flex-col pb-1">
              <span className="text-[12px] font-bold text-gray-400 mb-0.5 uppercase tracking-wider">MRP</span>
              <div className="flex items-center gap-3">
                <span className="text-2xl text-gray-400 font-bold line-through decoration-2 decoration-gray-300">{formatPrice(mrp, currency)}</span>
              </div>
            </div>
          )}
        </>
      ) : null}
    </div>
  );
}

