import { GenericPageData } from '../../pageInterface';
import { FaTable, FaPlus } from 'react-icons/fa';
import { createProjectsTableHook, createParzellenTableHook } from '../../../../hooks/usePekTables';
import { getUserDataCache } from '../../../../utils/userCache';

export const pekTablesPageData: GenericPageData = {
    id: 'pek-tables',
    path: 'start/pek-tables',
    name: 'Projektmanagement',
    description: 'Projektmanagement mit Tabellen',
    
    // Parent page
    parentPath: 'start',
    
    // Visual
    icon: FaTable,
    title: 'Projektmanagement',
    subtitle: 'Datenverwaltung',
    
    // Header buttons
    headerButtons: [
        {
            id: 'create-project',
            label: 'Neues Projekt',
            variant: 'primary',
            size: 'lg',
            icon: FaPlus,
            formConfig: {
                fields: [], // Will be generated from attributes via generateEditFieldsFromAttributes
                popupTitle: 'Neues Projekt erstellen',
                popupSize: 'large',
                createOperationName: 'handleProjectCreate',
                multiStep: true // Enable multi-step form with Step 1 (label) and Step 2 (parcel selection)
            },
            disabled: (hookData: any) => {
                if (!hookData?.permissions) return { disabled: false };
                const hasCreate = hookData.permissions.create !== 'n' && hookData.permissions.view;
                return { disabled: !hasCreate, message: 'No permission to create projects' };
            }
        }
    ],
    
    // Content sections
    content: [
        {
            id: 'projektmanagement-layout',
            type: 'columns',
            columnsConfig: {
                columns: [
                    {
                        id: 'main-column',
                        width: '3fr',
                        content: [
                            {
                                id: 'tables-tabs',
                                type: 'tabs',
                                tabsConfig: {
                                    tabs: [
                                        {
                                            id: 'projects',
                                            label: 'Projekte',
                                            content: [
                                                {
                                                    id: 'projects-table',
                                                    type: 'table',
                                                    tableConfig: {
                                                        hookFactory: createProjectsTableHook,
                                                        searchable: true,
                                                        filterable: true,
                                                        sortable: true,
                                                        resizable: true,
                                                        pagination: true,
                                                        pageSize: 10,
                                                        emptyMessage: 'Noch keine Projekte erstellt, erstelle jetzt dein erstes Projekt!',
                                                        actionButtons: [
                                                            {
                                                                type: 'edit',
                                                                title: 'common.edit',
                                                                idField: 'id',
                                                                operationName: 'handleProjectUpdate',
                                                                loadingStateName: 'editingProjects',
                                                                fetchItemFunctionName: 'fetchProjectById',
                                                                disabled: (hookData: any) => {
                                                                    if (!hookData?.permissions) return { disabled: false };
                                                                    const hasUpdate = hookData.permissions.update !== 'n' && hookData.permissions.view;
                                                                    return { disabled: !hasUpdate, message: 'No permission to edit projects' };
                                                                }
                                                            },
                                                            {
                                                                type: 'delete',
                                                                title: 'common.delete',
                                                                idField: 'id',
                                                                operationName: 'handleDelete',
                                                                loadingStateName: 'deletingProjects',
                                                                disabled: (hookData: any) => {
                                                                    if (!hookData?.permissions) return { disabled: false };
                                                                    const hasDelete = hookData.permissions.delete !== 'n' && hookData.permissions.view;
                                                                    return { disabled: !hasDelete, message: 'No permission to delete projects' };
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        },
                                        {
                                            id: 'parzellen',
                                            label: 'Parzellen',
                                            content: [
                                                {
                                                    id: 'parzellen-table',
                                                    type: 'table',
                                                    tableConfig: {
                                                        hookFactory: createParzellenTableHook,
                                                        searchable: true,
                                                        filterable: true,
                                                        sortable: true,
                                                        resizable: true,
                                                        pagination: true,
                                                        pageSize: 10,
                                                        emptyMessage: 'Noch keine Parzellen erstellt, erstelle jetzt dein erstes Projekt und füge eine Parzelle hinzu!',
                                                        actionButtons: [
                                                            {
                                                                type: 'view',
                                                                title: 'common.view',
                                                                idField: 'id',
                                                                nameField: 'label',
                                                                operationName: 'handleParzelleView',
                                                                loadingStateName: 'viewingParzellen',
                                                                disabled: (hookData: any) => {
                                                                    if (!hookData?.permissions) return { disabled: false };
                                                                    const hasRead = hookData.permissions.read !== 'n' && hookData.permissions.view;
                                                                    return { disabled: !hasRead, message: 'No permission to view parzellen' };
                                                                }
                                                            },
                                                            {
                                                                type: 'edit',
                                                                title: 'common.edit',
                                                                idField: 'id',
                                                                operationName: 'handleParzelleUpdate',
                                                                loadingStateName: 'editingParzellen',
                                                                fetchItemFunctionName: 'fetchParzelleById',
                                                                disabled: (hookData: any) => {
                                                                    if (!hookData?.permissions) return { disabled: false };
                                                                    const hasUpdate = hookData.permissions.update !== 'n' && hookData.permissions.view;
                                                                    return { disabled: !hasUpdate, message: 'No permission to edit parzellen' };
                                                                }
                                                            },
                                                            {
                                                                type: 'delete',
                                                                title: 'common.delete',
                                                                idField: 'id',
                                                                operationName: 'handleDelete',
                                                                loadingStateName: 'deletingParzellen',
                                                                disabled: (hookData: any) => {
                                                                    if (!hookData?.permissions) return { disabled: false };
                                                                    const hasDelete = hookData.permissions.delete !== 'n' && hookData.permissions.view;
                                                                    return { disabled: !hasDelete, message: 'No permission to delete parzellen' };
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            ]
                                        }
                                    ],
                                    defaultTabId: 'projects'
                                }
                            }
                        ]
                    },
                    {
                        id: 'sidebar-column',
                        width: '1fr',
                        content: []
                    }
                ],
                gap: '1rem'
            }
        }
    ],
    
    // Page behavior
    persistent: false,
    preload: false,
    preserveState: true,
    moduleEnabled: true,
    
    // Sidebar
    order: 11,
    
    // Privilege checker: deny access for "user" role
    privilegeChecker: async () => {
        const userData = getUserDataCache();
        const roleLabels = Array.isArray(userData?.roleLabels) ? userData.roleLabels : [];
        // Deny access if user has "user" role
        return !roleLabels.includes('user');
    },

    // Lifecycle hooks
    onActivate: async () => {
        if (import.meta.env.DEV) console.log('PEK Tables page activated');
    },
    onDeactivate: async () => {
        if (import.meta.env.DEV) console.log('PEK Tables page deactivated');
    }
};

