import type { ReactNode } from "react";
import {
  CheckCircleIcon,
  ExclamationTriangleIcon,
  ClockIcon,
  XCircleIcon,
  ServerIcon,
} from "@heroicons/react/24/outline";
import {
  SUBSCRIPTION_STATUS,
  SUBSCRIPTION_CYCLE,
  type SubscriptionStatus,
  type SubscriptionCycle,
} from "@customer-portal/domain/subscriptions";

export type SubscriptionStatusVariant = "success" | "info" | "warning" | "neutral" | "error";

const STATUS_ICON_MAP: Record<SubscriptionStatus, ReactNode> = {
  [SUBSCRIPTION_STATUS.ACTIVE]: <CheckCircleIcon className="h-6 w-6 text-green-500" />,
  [SUBSCRIPTION_STATUS.INACTIVE]: <ServerIcon className="h-6 w-6 text-gray-500" />,
  [SUBSCRIPTION_STATUS.PENDING]: <ClockIcon className="h-6 w-6 text-blue-500" />,
  [SUBSCRIPTION_STATUS.SUSPENDED]: <ExclamationTriangleIcon className="h-6 w-6 text-yellow-500" />,
  [SUBSCRIPTION_STATUS.TERMINATED]: <XCircleIcon className="h-6 w-6 text-red-500" />,
  [SUBSCRIPTION_STATUS.CANCELLED]: <XCircleIcon className="h-6 w-6 text-gray-500" />,
  [SUBSCRIPTION_STATUS.COMPLETED]: <CheckCircleIcon className="h-6 w-6 text-green-500" />,
};

const STATUS_VARIANT_MAP: Record<SubscriptionStatus, SubscriptionStatusVariant> = {
  [SUBSCRIPTION_STATUS.ACTIVE]: "success",
  [SUBSCRIPTION_STATUS.INACTIVE]: "neutral",
  [SUBSCRIPTION_STATUS.PENDING]: "info",
  [SUBSCRIPTION_STATUS.SUSPENDED]: "warning",
  [SUBSCRIPTION_STATUS.TERMINATED]: "error",
  [SUBSCRIPTION_STATUS.CANCELLED]: "neutral",
  [SUBSCRIPTION_STATUS.COMPLETED]: "success",
};

export function getSubscriptionStatusIcon(status: SubscriptionStatus): ReactNode {
  return STATUS_ICON_MAP[status] ?? <ServerIcon className="h-6 w-6 text-gray-500" />;
}

export function getSubscriptionStatusVariant(status: SubscriptionStatus): SubscriptionStatusVariant {
  return STATUS_VARIANT_MAP[status] ?? "neutral";
}

const BILLING_LABEL_MAP: Partial<Record<SubscriptionCycle, string>> = {
  [SUBSCRIPTION_CYCLE.MONTHLY]: "Monthly Billing",
  [SUBSCRIPTION_CYCLE.QUARTERLY]: "Quarterly Billing",
  [SUBSCRIPTION_CYCLE.SEMI_ANNUALLY]: "Semi-Annual Billing",
  [SUBSCRIPTION_CYCLE.ANNUALLY]: "Annual Billing",
  [SUBSCRIPTION_CYCLE.BIENNIALLY]: "Biennial Billing",
  [SUBSCRIPTION_CYCLE.TRIENNIALLY]: "Triennial Billing",
  [SUBSCRIPTION_CYCLE.ONE_TIME]: "One-time Payment",
  [SUBSCRIPTION_CYCLE.FREE]: "Free Plan",
};

export function getBillingCycleLabel(cycle: SubscriptionCycle): string {
  return BILLING_LABEL_MAP[cycle] ?? cycle;
}
