"use client";

import { forwardRef } from "react";
import { CalendarIcon, ArrowTopRightOnSquareIcon } from "@heroicons/react/24/outline";
import { StatusPill } from "@/components/atoms/status-pill";
import { Button } from "@/components/atoms/button";
import { SubCard } from "@/components/molecules/SubCard/SubCard";
import type { Subscription } from "@customer-portal/domain/subscriptions";

import { useFormatCurrency } from "@/lib/hooks/useFormatCurrency";
import { cn, formatIsoDate } from "@/lib/utils";
import {
  getBillingCycleLabel,
  getSubscriptionStatusIcon,
  getSubscriptionStatusVariant,
} from "@/features/subscriptions/utils/status-presenters";

interface SubscriptionCardProps {
  subscription: Subscription;
  variant?: "list" | "grid";
  showActions?: boolean;
  onViewClick?: (subscription: Subscription) => void;
  className?: string;
}

const formatDate = (dateString: string | undefined) => {
  return formatIsoDate(dateString);
};

export const SubscriptionCard = forwardRef<HTMLDivElement, SubscriptionCardProps>(
  ({ subscription, variant = "list", showActions = true, onViewClick, className }, ref) => {
    const { formatCurrency } = useFormatCurrency();

    const handleViewClick = () => {
      if (onViewClick) {
        onViewClick(subscription);
      }
    };

    if (variant === "grid") {
      return (
        <SubCard
          ref={ref}
          className={cn(
            "hover:shadow-lg hover:-translate-y-0.5 transition-all duration-200",
            className
          )}
        >
          <div className="space-y-4">
            {/* Header */}
            <div className="flex items-start justify-between">
              <div className="flex items-center space-x-3">
                {getSubscriptionStatusIcon(subscription.status)}
                <div className="min-w-0 flex-1">
                  <h3 className="text-lg font-semibold text-foreground truncate">
                    {subscription.productName}
                  </h3>
                  <p className="text-sm text-muted-foreground">
                    Service ID: {subscription.serviceId}
                  </p>
                </div>
              </div>
              <StatusPill
                label={subscription.status}
                variant={getSubscriptionStatusVariant(subscription.status)}
                size="sm"
              />
            </div>

            {/* Details */}
            <div className="grid grid-cols-2 gap-4 text-sm">
              <div>
                <p className="text-muted-foreground text-xs font-medium uppercase tracking-wider">
                  Price
                </p>
                <p className="font-semibold text-foreground mt-1">
                  {formatCurrency(subscription.amount)}
                </p>
                <p className="text-xs text-muted-foreground">
                  {getBillingCycleLabel(subscription.cycle)}
                </p>
              </div>
              <div>
                <p className="text-muted-foreground text-xs font-medium uppercase tracking-wider">
                  Next Due
                </p>
                <div className="flex items-center space-x-1 mt-1">
                  <CalendarIcon className="h-4 w-4 text-muted-foreground/60" />
                  <p className="font-medium text-foreground">{formatDate(subscription.nextDue)}</p>
                </div>
              </div>
            </div>

            {/* Actions */}
            {showActions && (
              <div className="flex items-center justify-between pt-3 border-t border-border/60">
                <p className="text-xs text-muted-foreground">
                  Created {formatDate(subscription.registrationDate)}
                </p>
                <div className="flex items-center space-x-2">
                  <Button
                    variant="outline"
                    size="sm"
                    onClick={handleViewClick}
                    rightIcon={<ArrowTopRightOnSquareIcon className="h-4 w-4" />}
                  >
                    View
                  </Button>
                </div>
              </div>
            )}
          </div>
        </SubCard>
      );
    }

    // List variant (default)
    return (
      <SubCard
        ref={ref}
        className={cn(
          "hover:shadow-lg hover:-translate-y-0.5 transition-all duration-200",
          className
        )}
      >
        <div className="flex items-center justify-between">
          <div className="flex items-center space-x-4 min-w-0 flex-1">
            {getSubscriptionStatusIcon(subscription.status)}
            <div className="min-w-0 flex-1">
              <div className="flex items-center space-x-3">
                <h3 className="text-base font-semibold text-foreground truncate">
                  {subscription.productName}
                </h3>
                <StatusPill
                  label={subscription.status}
                  variant={getSubscriptionStatusVariant(subscription.status)}
                  size="sm"
                />
              </div>
              <p className="text-sm text-muted-foreground mt-1">
                Service ID: {subscription.serviceId}
              </p>
            </div>
          </div>

          <div className="flex items-center space-x-6 text-sm">
            <div className="text-right">
              <p className="font-semibold text-foreground tabular-nums">
                {formatCurrency(subscription.amount)}
              </p>
              <p className="text-muted-foreground text-xs">
                {getBillingCycleLabel(subscription.cycle)}
              </p>
            </div>

            <div className="text-right hidden sm:block">
              <div className="flex items-center space-x-1">
                <CalendarIcon className="h-4 w-4 text-muted-foreground/60" />
                <p className="text-foreground">{formatDate(subscription.nextDue)}</p>
              </div>
              <p className="text-muted-foreground text-xs">Next due</p>
            </div>

            {showActions && (
              <div className="flex items-center space-x-2">
                <Button
                  variant="ghost"
                  size="sm"
                  onClick={handleViewClick}
                  rightIcon={<ArrowTopRightOnSquareIcon className="h-4 w-4" />}
                >
                  View
                </Button>
              </div>
            )}
          </div>
        </div>
      </SubCard>
    );
  }
);

SubscriptionCard.displayName = "SubscriptionCard";
