'use client';

import React from 'react';
import DynamicClientRow from './DynamicClientRow';
import { FALLBACK_COLUMNS, ALL_CLIENT_ACTIONS } from './useClientsPermissions';
import type { ButtonConfig } from './useClientsPermissions';
import { mapColumnKeyToValue } from '@/lib/api/mappers/clients.mappers';
import type { ClientRow as ClientRowType } from '@/lib/api/types/clients.types';

interface Props {
  row: ClientRowType;
  permittedActions: Set<string>;
  onView: (row: ClientRowType) => void;
  onEdit: (row: ClientRowType) => void;
  onDelete: (row: ClientRowType) => void;
  onAuditRequest: (row: ClientRowType) => void;
  onExportRow: (row: ClientRowType) => void;
  onOpenSignedDoc: (row: ClientRowType) => void;
}

const FALLBACK_LABELS: Record<string, string> = {
  view: 'View',
  edit: 'Edit',
  'audit-request': 'Audit request',
  export: 'Export row',
  delete: 'Delete',
  'signed-doc': 'Signed doc',
};

/**
 * Fallback row — used when /modules or /user-permissions is unreachable.
 * Renders the same cells as the dynamic row using FALLBACK_COLUMNS, and
 * builds its button list from whatever actions the caller passes in.
 */
export default function ClientRow(props: Props) {
  const { row, permittedActions, ...handlers } = props;

  const rowButtons: ButtonConfig[] = ALL_CLIENT_ACTIONS.filter(
    (k) => k !== 'view-all' && permittedActions.has(k),
  ).map((key, i) => ({
    key,
    icon: '',
    color: '',
    label: FALLBACK_LABELS[key] ?? key,
    order: i + 1,
    position: 'row' as const,
  }));

  return (
    <DynamicClientRow
      row={row}
      visibleColumns={FALLBACK_COLUMNS}
      rowButtons={rowButtons}
      hasActionsColumn
      mapColumnKeyToValue={mapColumnKeyToValue}
      {...handlers}
    />
  );
}
