import { CheckIcon, PowerIcon, PowerOffIcon, XIcon } from "lucide-react";
import type { LucideIcon } from "lucide-react";

import { ProviderStatus, VerificationStatus } from "@/types/provider";

/** Badge variants are the design-system status tokens — no ad-hoc colors */
type BadgeVariant =
  "under-review" | "complete" | "under-dispute" | "cancel" | "closed";

export const PROVIDER_STATUS_VARIANT: Record<ProviderStatus, BadgeVariant> = {
  [ProviderStatus.PENDING_VERIFICATION]: "under-review",
  [ProviderStatus.ACTIVE]: "complete",
  [ProviderStatus.SUSPENDED]: "under-dispute",
  [ProviderStatus.REJECTED]: "cancel",
};

export const DOCUMENT_STATUS_VARIANT: Record<VerificationStatus, BadgeVariant> =
  {
    [VerificationStatus.PENDING]: "under-review",
    [VerificationStatus.APPROVED]: "complete",
    [VerificationStatus.REJECTED]: "cancel",
  };

/** Tab order for the list screen — "all" plus every status, review queue first */
export const PROVIDER_STATUS_TABS = [
  ProviderStatus.PENDING_VERIFICATION,
  ProviderStatus.ACTIVE,
  ProviderStatus.SUSPENDED,
  ProviderStatus.REJECTED,
] as const;

export interface ProviderStatusAction {
  /** Target status sent to the API */
  status: ProviderStatus;
  /** Dictionary key under `providers.actions` */
  labelKey: string;
  /** Dictionary key prefix under `providers.confirm` */
  confirmKey: "approve" | "activate" | "deactivate" | "reject";
  /** Icon shown for this action in the row-actions column */
  icon: LucideIcon;
  destructive: boolean;
}

/** Pending → Active: approve the registration so the provider can trade */
const APPROVE: ProviderStatusAction = {
  status: ProviderStatus.ACTIVE,
  labelKey: "providers.actions.approve",
  confirmKey: "approve",
  icon: CheckIcon,
  destructive: false,
};

/** Pending → Rejected: turn the registration down (terminal in the UI) */
const REJECT: ProviderStatusAction = {
  status: ProviderStatus.REJECTED,
  labelKey: "providers.actions.reject",
  confirmKey: "reject",
  icon: XIcon,
  destructive: true,
};

/** Inactive → Active: re-enable an approved provider */
const ACTIVATE: ProviderStatusAction = {
  status: ProviderStatus.ACTIVE,
  labelKey: "providers.actions.activate",
  confirmKey: "activate",
  icon: PowerIcon,
  destructive: false,
};

/** Active → Inactive: pause an approved provider (SUSPENDED shows as "Inactive") */
const DEACTIVATE: ProviderStatusAction = {
  status: ProviderStatus.SUSPENDED,
  labelKey: "providers.actions.deactivate",
  confirmKey: "deactivate",
  icon: PowerOffIcon,
  destructive: true,
};

/**
 * Status changes offered from the provider's current state.
 *
 * The flow has two stages: a pending registration is either **approved**
 * (→ Active) or **rejected**, and rejection is terminal — no actions remain.
 * Once approved, the account only toggles between **Active** and **Inactive**
 * (SUSPENDED); reject is no longer offered.
 *
 * The API itself accepts other transitions, so this is a UX guard, not a
 * security one — it just surfaces the moves this product flow allows.
 */
export function getStatusActions(
  current: ProviderStatus,
): ProviderStatusAction[] {
  switch (current) {
    case ProviderStatus.PENDING_VERIFICATION:
      return [APPROVE, REJECT];
    case ProviderStatus.ACTIVE:
      return [DEACTIVATE];
    case ProviderStatus.SUSPENDED:
      return [ACTIVATE];
    case ProviderStatus.REJECTED:
      return [];
    default:
      return [];
  }
}

/** `null` means the platform default applies rather than a per-provider rate */
export function formatCommissionRate(rate: number | null): string | null {
  if (rate === null || Number.isNaN(rate)) return null;
  return `${Number(rate.toFixed(2))}%`;
}
