/**
 * Global Table Actions Configuration
 * Defines bulk actions and row actions for all table-based pages
 * Delete action is always present - other actions are page-specific
 */

import { TrashBinIcon, PencilIcon, CheckCircleIcon, DownloadIcon, BoltIcon, ArrowRightIcon } from '../../icons';
import React from 'react';

export interface RowActionConfig {
  key: string; // 'edit' | 'delete' | custom
  label: string;
  icon: React.ReactNode;
  variant?: 'primary' | 'danger' | 'secondary' | 'success'; // For styling
}

export interface BulkActionConfig {
  key: string; // 'export' | 'update_status' | 'delete' | custom
  label: string;
  icon: React.ReactNode;
  variant?: 'primary' | 'danger' | 'secondary' | 'success'; // For styling
}

export interface TableActionsConfig {
  // Single record actions (edit is always first, delete is always last)
  rowActions: RowActionConfig[];
  // Bulk actions (delete is always last)
  bulkActions: BulkActionConfig[];
}

// Global icons
const EditIcon = <PencilIcon className="w-5 h-5" />;
const DeleteIcon = <TrashBinIcon className="w-5 h-5" />;
const ExportIcon = <DownloadIcon className="w-5 h-5" />;

/**
 * Get table actions config for a given route
 * Delete action is always included automatically (except for keyword-opportunities)
 */
export function getTableActionsConfig(route: string): TableActionsConfig {
  const config = tableActionsConfigs[route] || tableActionsConfigs['default'];
  
  // Special handling for keyword-opportunities: no delete/export/edit
  if (route === '/planner/keyword-opportunities') {
    return {
      rowActions: config.rowActions || [],
      bulkActions: config.bulkActions || [],
    };
  }
  
  // Build row actions: Update (Edit), then custom actions, then Export, then Delete
  // Always include: Update, Delete, Export
  const rowActionsWithoutDefaults = config.rowActions.filter(
    a => a.key !== 'edit' && a.key !== 'delete' && a.key !== 'export'
  );
  
  // Always include Update (Edit) as first action
  const updateAction = config.rowActions.find(a => a.key === 'edit' || a.key === 'update') || {
    key: 'edit',
    label: 'Update',
    icon: EditIcon,
    variant: 'primary' as const,
  };
  
  // Always include Export before Delete
  const exportAction = config.rowActions.find(a => a.key === 'export') || {
    key: 'export',
    label: 'Export',
    icon: ExportIcon,
    variant: 'secondary' as const,
  };
  
  // Always include Delete as last action (used for both row and bulk actions)
  const deleteAction = {
    key: 'delete',
    label: 'Delete',
    icon: DeleteIcon,
    variant: 'danger' as const,
  };
  
  // Ensure delete is always the last action for bulk actions
  const bulkActionsWithoutDelete = config.bulkActions.filter(a => a.key !== 'delete');
  const bulkDeleteAction = config.bulkActions.find(a => a.key === 'delete') || deleteAction;
  
  return {
    rowActions: [
      updateAction, // Update (Edit) always first
      ...rowActionsWithoutDefaults, // Custom actions from config
      exportAction, // Export always before delete
      deleteAction, // Delete always last
    ],
    bulkActions: [
      ...bulkActionsWithoutDelete,
      bulkDeleteAction, // Delete always last
    ],
  };
}

// Page-specific action configurations
const tableActionsConfigs: Record<string, TableActionsConfig> = {
  '/planner/keyword-opportunities': {
    rowActions: [
      {
        key: 'add_to_workflow',
        label: 'Add',
        icon: <BoltIcon className="w-5 h-5" />,
        variant: 'success',
      },
    ],
    bulkActions: [
      {
        key: 'add_selected_to_workflow',
        label: 'Add Selected to Workflow',
        icon: <BoltIcon className="w-5 h-5" />,
        variant: 'success',
      },
    ],
  },
  '/planner/keywords': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
      {
        key: 'auto_cluster',
        label: 'Auto-Cluster',
        icon: <BoltIcon className="w-4 h-4 text-warning-500" />,
        variant: 'secondary',
      },
    ],
  },
  '/planner/clusters': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
      {
        key: 'generate_ideas',
        label: 'Generate Ideas',
        icon: <BoltIcon className="w-5 h-5" />,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
      {
        key: 'auto_generate_ideas',
        label: 'Generate Ideas',
        icon: <BoltIcon className="w-4 h-4 text-warning-500" />,
        variant: 'secondary',
      },
    ],
  },
  '/planner/ideas': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
      {
        key: 'queue_to_writer',
        label: 'Queue to Writer',
        icon: <ArrowRightIcon className="w-5 h-5" />,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
      {
        key: 'queue_to_writer',
        label: 'Queue to Writer',
        icon: <ArrowRightIcon className="w-4 h-4 text-brand-500" />,
        variant: 'secondary',
      },
    ],
  },
  '/writer/tasks': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
      {
        key: 'generate_content',
        label: 'Generate Content',
        icon: <BoltIcon className="w-5 h-5" />,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
      // Removed generate_content from bulk actions - only available as row action
    ],
  },
  '/writer/content': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
      {
        key: 'generate_image_prompts',
        label: 'Generate Image Prompts',
        icon: <BoltIcon className="w-5 h-5 text-purple-500" />,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
      {
        key: 'publish',
        label: 'Publish Selected',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
    ],
  },
  '/writer/published': {
    rowActions: [
      {
        key: 'edit',
        label: 'Edit',
        icon: EditIcon,
        variant: 'primary',
      },
    ],
    bulkActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-4 h-4 text-success-500" />,
        variant: 'secondary',
      },
      {
        key: 'export',
        label: 'Export Selected',
        icon: <DownloadIcon className="w-4 h-4 text-blue-light-500" />,
        variant: 'secondary',
      },
    ],
  },
  '/writer/images': {
    rowActions: [
      {
        key: 'update_status',
        label: 'Update Status',
        icon: <CheckCircleIcon className="w-5 h-5" />,
        variant: 'primary',
      },
    ],
    bulkActions: [],
  },
  // Default config (fallback)
  default: {
    rowActions: [],
    bulkActions: [],
  },
};

