import { PackageCheck, RotateCcw, ShieldCheck, Truck } from "lucide-react";

import { cn } from "@/lib/utils";
import { formatPrice } from "@alqove/shared";
import type { ItemStorePoliciesData } from "@alqove/types";

export interface ShippingReturnsCardProps {
  policies?: ItemStorePoliciesData | null;
  storeCity?: string | null;
  storeState?: string | null;
  className?: string;
}

function formatLocation(city?: string | null, state?: string | null): string | null {
  const parts = [city, state].map((part) => part?.trim()).filter(Boolean);
  return parts.length > 0 ? parts.join(", ") : null;
}

function formatProcessingDays(days?: number | null): string {
  if (days == null) return "Processing time set by seller";
  if (days <= 0) return "Usually ready to ship now";
  return `Ships in ${days} business day${days === 1 ? "" : "s"}`;
}

function formatShippingRate(policies?: ItemStorePoliciesData | null): string {
  const rate = policies?.flat_shipping_rate;
  const threshold = policies?.free_shipping_threshold;

  if (rate == null && threshold == null) return "Shipping calculated at checkout";
  if (rate === 0) return "Free shipping";

  const rateText = rate != null ? `${formatPrice(rate)} flat shipping` : "Shipping calculated at checkout";
  if (threshold != null) return `${rateText}; free over ${formatPrice(threshold)}`;

  return rateText;
}

function formatReturns(days?: number | null): string {
  if (days == null) return "Returns reviewed under buyer protection";
  if (days <= 0) return "Final sale unless the item is not as described";
  return `${days}-day return window`;
}

export function ShippingReturnsCard({
  policies,
  storeCity,
  storeState,
  className,
}: ShippingReturnsCardProps) {
  const location = formatLocation(storeCity, storeState);
  const minimumOrder = policies?.minimum_order_amount;
  const policyText = policies?.return_policy_text?.trim();

  return (
    <section
      aria-labelledby="shipping-returns-heading"
      className={cn("rounded-lg border border-slate-200 bg-white p-5", className)}
    >
      <h2
        id="shipping-returns-heading"
        className="text-sm font-semibold uppercase tracking-wide text-slate-500"
      >
        Shipping and returns
      </h2>

      <dl className="mt-4 space-y-4">
        <div className="flex gap-3">
          <Truck aria-hidden="true" className="mt-0.5 size-4 shrink-0 text-forest-700" />
          <div>
            <dt className="text-sm font-semibold text-slate-900">Shipping</dt>
            <dd className="mt-0.5 text-sm leading-relaxed text-slate-600">
              {formatShippingRate(policies)}
              {location ? <span className="block">Ships from {location}</span> : null}
            </dd>
          </div>
        </div>

        <div className="flex gap-3">
          <PackageCheck aria-hidden="true" className="mt-0.5 size-4 shrink-0 text-slate-500" />
          <div>
            <dt className="text-sm font-semibold text-slate-900">Processing</dt>
            <dd className="mt-0.5 text-sm leading-relaxed text-slate-600">
              {formatProcessingDays(policies?.processing_days)}
              {minimumOrder != null ? (
                <span className="block">Minimum order {formatPrice(minimumOrder)}</span>
              ) : null}
            </dd>
          </div>
        </div>

        <div className="flex gap-3">
          <RotateCcw aria-hidden="true" className="mt-0.5 size-4 shrink-0 text-slate-500" />
          <div>
            <dt className="text-sm font-semibold text-slate-900">Returns</dt>
            <dd className="mt-0.5 text-sm leading-relaxed text-slate-600">
              {formatReturns(policies?.return_window_days)}
            </dd>
          </div>
        </div>

        <div className="flex gap-3">
          <ShieldCheck aria-hidden="true" className="mt-0.5 size-4 shrink-0 text-forest-700" />
          <div>
            <dt className="text-sm font-semibold text-slate-900">Buyer protection</dt>
            <dd className="mt-0.5 text-sm leading-relaxed text-slate-600">
              Covered if the item arrives damaged or not as described.
            </dd>
          </div>
        </div>
      </dl>

      {policyText ? (
        <p className="mt-4 rounded-md bg-slate-50 px-3 py-3 text-sm leading-relaxed text-slate-600">
          {policyText}
        </p>
      ) : null}
    </section>
  );
}
