/**
 * AccessRulesTable
 * 
 * Checkbox-based compact table for editing RBAC access rules.
 * Shows all permissions in a matrix format similar to Unix permissions.
 */

import React from 'react';
import { FaTable, FaDesktop, FaServer, FaTrash } from 'react-icons/fa';
import { type AccessRule, type RuleContext, type AccessLevel } from '../../hooks/useAccessRules';
import styles from './AccessRules.module.css';

// =============================================================================
// TYPES
// =============================================================================

interface AccessRulesTableProps {
  rules: AccessRule[];
  context: RuleContext;
  readOnly?: boolean;
  onUpdate: (ruleId: string, updates: Partial<AccessRule>) => void;
  onDelete: (ruleId: string) => void;
}

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

/**
 * Check if access level is at least the specified minimum level.
 * Hierarchy: n (none) < m (mine) < g (group) < a (all)
 */
const hasLevel = (level: AccessLevel | null | undefined, minLevel: 'm' | 'g' | 'a'): boolean => {
  if (!level || level === 'n') return false;
  const hierarchy = ['n', 'm', 'g', 'a'];
  return hierarchy.indexOf(level) >= hierarchy.indexOf(minLevel);
};

/**
 * Calculate the new access level when a checkbox is toggled.
 */
const calculateNewLevel = (
  currentLevel: AccessLevel | null | undefined,
  targetLevel: 'm' | 'g' | 'a',
  checked: boolean
): AccessLevel => {
  if (checked) {
    // Activating: set to target level
    return targetLevel;
  } else {
    // Deactivating: set to level below target
    const hierarchy: AccessLevel[] = ['n', 'm', 'g', 'a'];
    const targetIndex = hierarchy.indexOf(targetLevel);
    return hierarchy[targetIndex - 1] || 'n';
  }
};

// =============================================================================
// RULE ROW COMPONENT
// =============================================================================

interface AccessRuleRowProps {
  rule: AccessRule;
  isDataContext: boolean;
  readOnly?: boolean;
  onUpdate: (ruleId: string, updates: Partial<AccessRule>) => void;
  onDelete: (ruleId: string) => void;
}

const AccessRuleRow: React.FC<AccessRuleRowProps> = ({
  rule,
  isDataContext,
  readOnly,
  onUpdate,
  onDelete,
}) => {
  const handleLevelToggle = (
    field: 'read' | 'create' | 'update' | 'delete',
    targetLevel: 'm' | 'g' | 'a',
    checked: boolean
  ) => {
    const currentLevel = rule[field] as AccessLevel | null | undefined;
    const newLevel = calculateNewLevel(currentLevel, targetLevel, checked);
    onUpdate(rule.id, { [field]: newLevel });
  };

  // Get icon for context
  const getContextIcon = () => {
    switch (rule.context) {
      case 'DATA': return <FaTable />;
      case 'UI': return <FaDesktop />;
      case 'RESOURCE': return <FaServer />;
      default: return <FaTable />;
    }
  };

  return (
    <tr className={styles.ruleRow}>
      {/* Object Name */}
      <td className={styles.objectCell}>
        <span className={styles.objectIcon}>{getContextIcon()}</span>
        <code className={styles.objectCode}>{rule.item || '(global)'}</code>
      </td>
      
      {/* View Checkbox */}
      <td className={styles.checkboxCell}>
        <input
          type="checkbox"
          checked={rule.view}
          onChange={(e) => onUpdate(rule.id, { view: e.target.checked })}
          disabled={readOnly}
          title="Sichtbar"
        />
      </td>
      
      {/* CRUD Checkboxes for DATA context */}
      {isDataContext && (
        <>
          {/* EIGENE (m) */}
          {(['create', 'read', 'update', 'delete'] as const).map(op => (
            <td key={`m-${op}`} className={styles.checkboxCell}>
              <input
                type="checkbox"
                checked={hasLevel(rule[op] as AccessLevel, 'm')}
                onChange={(e) => handleLevelToggle(op, 'm', e.target.checked)}
                disabled={readOnly}
                title={`${op.charAt(0).toUpperCase() + op.slice(1)} - Eigene`}
              />
            </td>
          ))}
          
          {/* GRUPPE (g) */}
          {(['create', 'read', 'update', 'delete'] as const).map(op => (
            <td key={`g-${op}`} className={styles.checkboxCell}>
              <input
                type="checkbox"
                checked={hasLevel(rule[op] as AccessLevel, 'g')}
                onChange={(e) => handleLevelToggle(op, 'g', e.target.checked)}
                disabled={readOnly}
                title={`${op.charAt(0).toUpperCase() + op.slice(1)} - Gruppe`}
              />
            </td>
          ))}
          
          {/* ALLE (a) */}
          {(['create', 'read', 'update', 'delete'] as const).map(op => (
            <td key={`a-${op}`} className={styles.checkboxCell}>
              <input
                type="checkbox"
                checked={hasLevel(rule[op] as AccessLevel, 'a')}
                onChange={(e) => handleLevelToggle(op, 'a', e.target.checked)}
                disabled={readOnly}
                title={`${op.charAt(0).toUpperCase() + op.slice(1)} - Alle`}
              />
            </td>
          ))}
        </>
      )}
      
      {/* Delete Button */}
      <td className={styles.actionsCell}>
        {!readOnly && (
          <button
            className={`${styles.iconButton} ${styles.danger}`}
            onClick={() => onDelete(rule.id)}
            title="Regel löschen"
          >
            <FaTrash />
          </button>
        )}
      </td>
    </tr>
  );
};

// =============================================================================
// MAIN TABLE COMPONENT
// =============================================================================

export const AccessRulesTable: React.FC<AccessRulesTableProps> = ({
  rules,
  context,
  readOnly,
  onUpdate,
  onDelete,
}) => {
  const isDataContext = context === 'DATA';
  
  if (rules.length === 0) {
    return null;
  }
  
  return (
    <div className={styles.tableWrapper}>
      <table className={styles.accessRulesTable}>
        <thead>
          <tr>
            <th className={styles.colObject}>Objekt (Dot-Notation)</th>
            <th className={styles.colView}>View</th>
            {isDataContext && (
              <>
                <th className={styles.colGroupHeader} colSpan={4}>Eigene (m)</th>
                <th className={styles.colGroupHeader} colSpan={4}>Gruppe (g)</th>
                <th className={styles.colGroupHeader} colSpan={4}>Alle (a)</th>
              </>
            )}
            <th className={styles.colActions}></th>
          </tr>
          {isDataContext && (
            <tr className={styles.subHeader}>
              <th></th>
              <th></th>
              <th title="Create">C</th>
              <th title="Read">R</th>
              <th title="Update">U</th>
              <th title="Delete">D</th>
              <th title="Create">C</th>
              <th title="Read">R</th>
              <th title="Update">U</th>
              <th title="Delete">D</th>
              <th title="Create">C</th>
              <th title="Read">R</th>
              <th title="Update">U</th>
              <th title="Delete">D</th>
              <th></th>
            </tr>
          )}
        </thead>
        <tbody>
          {rules.map(rule => (
            <AccessRuleRow
              key={rule.id}
              rule={rule}
              isDataContext={isDataContext}
              readOnly={readOnly}
              onUpdate={onUpdate}
              onDelete={onDelete}
            />
          ))}
        </tbody>
      </table>
    </div>
  );
};

export default AccessRulesTable;
