"use client";

import Link from "next/link";
import { buildCallCopilotHref, openCallCopilotPopup, type CallCopilotLaunchContext } from "@/components/cori/call-copilot-support";

function ActionLink({ href, label, tone = "default", popupContext }: { href: string; label: string; tone?: "default" | "primary" | "danger"; popupContext?: CallCopilotLaunchContext }) {
  const tones = {
    default: "border bg-white text-gray-800 hover:bg-gray-50",
    primary: "bg-black text-white hover:bg-gray-800",
    danger: "border border-red-200 bg-red-50 text-red-700 hover:bg-red-100",
  } as const;

  return (
    <Link
      href={popupContext ? buildCallCopilotHref(popupContext) : href}
      onClick={(event) => {
        if (!popupContext) return;
        if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || event.button !== 0) return;
        event.preventDefault();
        openCallCopilotPopup(popupContext);
      }}
      className={`rounded-xl px-4 py-2 text-sm font-medium ${tones[tone]}`}
    >
      {label}
    </Link>
  );
}

function ActionButton({ onClick, label, disabled, tone = "default" }: { onClick: () => void; label: string; disabled?: boolean; tone?: "default" | "danger" }) {
  const tones = {
    default: "border bg-white text-gray-800 hover:bg-gray-50",
    danger: "border border-red-200 bg-red-50 text-red-700 hover:bg-red-100",
  } as const;

  return (
    <button type="button" onClick={onClick} disabled={disabled} className={`rounded-xl px-4 py-2 text-sm font-medium disabled:cursor-not-allowed disabled:opacity-60 ${tones[tone]}`}>
      {label}
    </button>
  );
}

export function CrmDetailActionBar({
  primaryActions,
  relationshipActions = [],
  jumpActions = [],
  stateAction,
}: {
  primaryActions: Array<{ href: string; label: string; tone?: "default" | "primary"; popupContext?: CallCopilotLaunchContext }>;
  relationshipActions?: Array<{ href: string; label: string }>;
  jumpActions?: Array<{ href: string; label: string }>;
  stateAction?: { label: string; tone?: "default" | "danger"; disabled?: boolean; onClick: () => void };
}) {
  return (
    <section className="rounded-2xl border bg-white p-4 shadow-sm">
      <div className="grid gap-4 xl:grid-cols-[1.4fr_1fr_1fr_auto]">
        <div>
          <div className="text-xs font-semibold uppercase tracking-wide text-gray-500">Primary actions</div>
          <div className="mt-3 flex flex-wrap gap-2">
            {primaryActions.map((action) => (
              <ActionLink key={`${action.label}-${action.href}`} href={action.href} label={action.label} tone={action.tone} popupContext={action.popupContext} />
            ))}
          </div>
        </div>

        <div>
          <div className="text-xs font-semibold uppercase tracking-wide text-gray-500">Related records</div>
          <div className="mt-3 flex flex-wrap gap-2">
            {relationshipActions.length ? relationshipActions.map((action) => (
              <ActionLink key={`${action.label}-${action.href}`} href={action.href} label={action.label} />
            )) : <div className="text-sm text-gray-500">No linked record shortcuts.</div>}
          </div>
        </div>

        <div>
          <div className="text-xs font-semibold uppercase tracking-wide text-gray-500">Jump links</div>
          <div className="mt-3 flex flex-wrap gap-2">
            {jumpActions.map((action) => (
              <ActionLink key={`${action.label}-${action.href}`} href={action.href} label={action.label} />
            ))}
          </div>
        </div>

        {stateAction ? (
          <div>
            <div className="text-xs font-semibold uppercase tracking-wide text-gray-500">Lifecycle</div>
            <div className="mt-3 flex flex-wrap gap-2 xl:justify-end">
              <ActionButton onClick={stateAction.onClick} label={stateAction.label} disabled={stateAction.disabled} tone={stateAction.tone} />
            </div>
          </div>
        ) : null}
      </div>
    </section>
  );
}
