import * as React from 'react';
import {
  Box,
  Typography,
  Button,
  IconButton,
  Link,
  Tooltip,
} from '@mui/material';
import {
  DataGrid,
  GridColDef,
  GridActionsCellItem,
  GridRenderCellParams,
} from '@mui/x-data-grid';
import EditIcon from '@mui/icons-material/Edit';
import DeleteIcon from '@mui/icons-material/Delete';
import { ResourceConfig, ResourceField } from '../types/config';

interface EnhancedTableProps {
  config: ResourceConfig;
  data: any[];
  onEdit: (item: any) => void;
  onDelete: (id: string) => void;
  onCreate: () => void;
  onNavigateToResource?: (resourceName: string, id: string) => void;
}

export default function EnhancedTable({
  config,
  data,
  onEdit,
  onDelete,
  onCreate,
  onNavigateToResource,
}: EnhancedTableProps) {
  
  const columns: GridColDef[] = React.useMemo(() => {
    const cols: GridColDef[] = Object.entries(config.fields).map(([key, field]) => {
      const col: GridColDef = {
        field: key,
        headerName: field.label,
        flex: 1,
        minWidth: 150,
        renderCell: (params: GridRenderCellParams) => {
          const value = params.value;
          
          // 1. Custom Formatter
          if (field.formatter) {
            return field.formatter(value);
          }

          // 2. Relational Link
          if (field.relation && value) {
            const relationId = typeof value === 'object' ? value.id : value;
            if (relationId) {
                return (
                    <Link
                      component="button"
                      variant="body2"
                      onClick={(e) => {
                        e.stopPropagation();
                        onNavigateToResource?.(field.relation!, relationId);
                      }}
                    >
                      {relationId}
                    </Link>
                  );
            }
          }

          // 3. Nested Object / Array Display
          if (field.type === 'array' && Array.isArray(value)) {
            if (field.displayField) {
              return value
                .map((item) => (typeof item === 'object' ? item[field.displayField!] : item))
                .filter(Boolean)
                .join(', ');
            }
            return `${value.length} items`;
          }

          if (field.type === 'object' && value) {
            if (field.displayField && value[field.displayField]) {
                return value[field.displayField];
            }
            return JSON.stringify(value);
          }

          // 4. Default renderings
          if (field.type === 'boolean') return value ? 'Yes' : 'No';
          if (field.type === 'datetime' || field.type === 'date') {
            return new Date(value).toLocaleString();
          }

          return value;
        }
      };
      return col;
    });

    cols.push({
      field: 'actions',
      type: 'actions',
      headerName: 'Actions',
      width: 100,
      getActions: (params) => [
        <GridActionsCellItem
          icon={<EditIcon />}
          label="Edit"
          onClick={() => onEdit(params.row)}
        />,
        <GridActionsCellItem
          icon={<DeleteIcon />}
          label="Delete"
          onClick={() => onDelete(params.id as string)}
        />,
      ],
    });

    return cols;
  }, [config, onEdit, onDelete, onNavigateToResource]);

  return (
    <Box sx={{ height: 600, width: '100%' }}>
      <Box sx={{ display: 'flex', justifyContent: 'space-between', mb: 3, alignItems: 'center' }}>
        <Typography variant="h5">{config.pluralLabel}</Typography>
        <Button variant="contained" color="primary" onClick={onCreate}>
          Add {config.label}
        </Button>
      </Box>
      <DataGrid
        rows={data || []}
        columns={columns}
        getRowId={(row) => {
            const pk = config.primaryKey;
            if (row[pk] !== undefined && row[pk] !== null) return row[pk];
            // Fallback: search for common ID fields
            const fallbackKeys = ['id', 'uuid', 'pk'];
            for (const key of fallbackKeys) {
                if (row[key] !== undefined && row[key] !== null) return row[key];
            }
            debugger;

            // Absolute fallback: index (not ideal but avoids crash)
            return `temp-id-${data.indexOf(row)}`;
        }}
        disableRowSelectionOnClick
        initialState={{
          pagination: {
            paginationModel: { page: 0, pageSize: 10 },
          },
        }}
        pageSizeOptions={[5, 10, 25]}
      />
    </Box>
  );
}
