import {
  Plus,
  FileEdit,
  Trash2,
  LogIn,
  ArrowRight,
  Send,
  Users,
  UserPlus,
  Upload,
  Eye,
} from 'lucide-react'
import { createElement } from 'react'

export function formatEntity(entityType: string | null): string {
  if (!entityType) return 'record'
  return entityType
    .replace(/([a-z])([A-Z])/g, '$1 $2')
    .replace(/_/g, ' ')
    .toLowerCase()
}

export function formatAction(action: string, entityType: string | null): string {
  const entity = formatEntity(entityType)
  const actionMap: Record<string, string> = {
    CREATE: `created a ${entity}`,
    UPDATE: `updated a ${entity}`,
    DELETE: `deleted a ${entity}`,
    IMPORT: `imported ${entity}s`,
    EXPORT: `exported ${entity} data`,
    REORDER: `reordered ${entity}s`,
    LOGIN: 'logged in',
    LOGIN_SUCCESS: 'logged in',
    LOGIN_FAILED: 'failed to log in',
    PASSWORD_SET: 'set their password',
    PASSWORD_CHANGED: 'changed their password',
    REQUEST_PASSWORD_RESET: 'requested a password reset',
    COMPLETE_ONBOARDING: 'completed onboarding',
    DELETE_OWN_ACCOUNT: 'deleted their account',
    EVALUATION_SUBMITTED: 'submitted an evaluation',
    COI_DECLARED: 'declared a conflict of interest',
    COI_NO_CONFLICT: 'confirmed no conflict of interest',
    COI_REVIEWED: 'reviewed a COI declaration',
    REMINDERS_TRIGGERED: 'triggered evaluation reminders',
    DISCUSSION_COMMENT_ADDED: 'added a discussion comment',
    DISCUSSION_CLOSED: 'closed a discussion',
    ASSIGN: `assigned a ${entity}`,
    BULK_CREATE: `bulk created ${entity}s`,
    BULK_ASSIGN: 'bulk assigned users',
    BULK_DELETE: `bulk deleted ${entity}s`,
    BULK_UPDATE: `bulk updated ${entity}s`,
    BULK_UPDATE_STATUS: 'bulk updated statuses',
    APPLY_SUGGESTIONS: 'applied assignment suggestions',
    ASSIGN_PROJECTS_TO_ROUND: 'assigned projects to round',
    REMOVE_PROJECTS_FROM_ROUND: 'removed projects from round',
    ADVANCE_PROJECTS: 'advanced projects to next round',
    BULK_ASSIGN_TO_ROUND: 'bulk assigned to round',
    REORDER_ROUNDS: 'reordered rounds',
    STATUS_CHANGE: `changed ${entity} status`,
    UPDATE_STATUS: `updated ${entity} status`,
    ROLE_CHANGED: 'changed a user role',
    INVITE: 'invited a user',
    SEND_INVITATION: 'sent an invitation',
    BULK_SEND_INVITATIONS: 'sent bulk invitations',
    UPLOAD_FILE: 'uploaded a file',
    DELETE_FILE: 'deleted a file',
    REPLACE_FILE: 'replaced a file',
    FILE_DOWNLOADED: 'downloaded a file',
    EXECUTE_FILTERING: 'ran project filtering',
    FINALIZE_FILTERING: 'finalized filtering results',
    OVERRIDE: `overrode a ${entity} result`,
    BULK_OVERRIDE: 'bulk overrode filtering results',
    REINSTATE: 'reinstated a project',
    BULK_REINSTATE: 'bulk reinstated projects',
    AI_TAG: 'ran AI tagging',
    START_AI_TAG_JOB: 'started AI tagging job',
    EVALUATION_SUMMARY: 'generated an AI summary',
    AWARD_ELIGIBILITY: 'ran award eligibility check',
    PROJECT_TAGGING: 'ran project tagging',
    FILTERING: 'ran AI filtering',
    MENTOR_MATCHING: 'ran mentor matching',
    ADD_TAG: 'added a tag',
    REMOVE_TAG: 'removed a tag',
    BULK_CREATE_TAGS: 'bulk created tags',
    MENTOR_ASSIGN: 'assigned a mentor',
    MENTOR_UNASSIGN: 'unassigned a mentor',
    MENTOR_AUTO_ASSIGN: 'auto-assigned mentors',
    MENTOR_BULK_ASSIGN: 'bulk assigned mentors',
    CREATE_MENTOR_NOTE: 'created a mentor note',
    COMPLETE_MILESTONE: 'completed a milestone',
    SEND_MESSAGE: 'sent a message',
    CREATE_MESSAGE_TEMPLATE: 'created a message template',
    UPDATE_MESSAGE_TEMPLATE: 'updated a message template',
    DELETE_MESSAGE_TEMPLATE: 'deleted a message template',
    CREATE_WEBHOOK: 'created a webhook',
    UPDATE_WEBHOOK: 'updated a webhook',
    DELETE_WEBHOOK: 'deleted a webhook',
    TEST_WEBHOOK: 'tested a webhook',
    REGENERATE_WEBHOOK_SECRET: 'regenerated a webhook secret',
    UPDATE_SETTING: 'updated a setting',
    UPDATE_SETTINGS_BATCH: 'updated settings',
    UPDATE_NOTIFICATION_PREFERENCES: 'updated notification preferences',
    UPDATE_DIGEST_SETTINGS: 'updated digest settings',
    UPDATE_ANALYTICS_SETTINGS: 'updated analytics settings',
    UPDATE_AUDIT_SETTINGS: 'updated audit settings',
    UPDATE_LOCALIZATION_SETTINGS: 'updated localization settings',
    UPDATE_RETENTION_CONFIG: 'updated retention config',
    START_VOTING: 'started live voting',
    END_SESSION: 'ended a live voting session',
    UPDATE_SESSION_CONFIG: 'updated session config',
    CREATE_ROUND_TEMPLATE: 'created a round template',
    CREATE_ROUND_TEMPLATE_FROM_ROUND: 'saved round as template',
    UPDATE_ROUND_TEMPLATE: 'updated a round template',
    DELETE_ROUND_TEMPLATE: 'deleted a round template',
    UPDATE_EVALUATION_FORM: 'updated the evaluation form',
    GRANT_GRACE_PERIOD: 'granted a grace period',
    UPDATE_GRACE_PERIOD: 'updated a grace period',
    REVOKE_GRACE_PERIOD: 'revoked a grace period',
    BULK_GRANT_GRACE_PERIOD: 'bulk granted grace periods',
    SET_AWARD_WINNER: 'set an award winner',
    REPORT_GENERATED: 'generated a report',
    DRAFT_SUBMITTED: 'submitted a draft application',
    SUBMIT: `submitted a ${entity}`,
  }
  if (actionMap[action]) return actionMap[action]
  return action.toLowerCase().replace(/_/g, ' ')
}

const iconClass = 'h-3 w-3'

export function getActionIcon(action: string) {
  switch (action) {
    case 'CREATE':
    case 'BULK_CREATE':
      return createElement(Plus, { className: iconClass })
    case 'UPDATE':
    case 'UPDATE_STATUS':
    case 'BULK_UPDATE':
    case 'BULK_UPDATE_STATUS':
    case 'STATUS_CHANGE':
    case 'ROLE_CHANGED':
      return createElement(FileEdit, { className: iconClass })
    case 'DELETE':
    case 'BULK_DELETE':
      return createElement(Trash2, { className: iconClass })
    case 'LOGIN':
    case 'LOGIN_SUCCESS':
    case 'LOGIN_FAILED':
    case 'PASSWORD_SET':
    case 'PASSWORD_CHANGED':
    case 'COMPLETE_ONBOARDING':
      return createElement(LogIn, { className: iconClass })
    case 'EXPORT':
    case 'REPORT_GENERATED':
      return createElement(ArrowRight, { className: iconClass })
    case 'SUBMIT':
    case 'EVALUATION_SUBMITTED':
    case 'DRAFT_SUBMITTED':
      return createElement(Send, { className: iconClass })
    case 'ASSIGN':
    case 'BULK_ASSIGN':
    case 'APPLY_SUGGESTIONS':
    case 'ASSIGN_PROJECTS_TO_ROUND':
    case 'MENTOR_ASSIGN':
    case 'MENTOR_BULK_ASSIGN':
      return createElement(Users, { className: iconClass })
    case 'INVITE':
    case 'SEND_INVITATION':
    case 'BULK_SEND_INVITATIONS':
      return createElement(UserPlus, { className: iconClass })
    case 'IMPORT':
      return createElement(Upload, { className: iconClass })
    default:
      return createElement(Eye, { className: iconClass })
  }
}
