import { NavigationNode } from '../../types/navigation';
import { getShapeType, CCNodeTypes } from '../../utils/tldraw/cc-base/cc-graph/cc-graph-types';
import { getThemeFromLabel } from '../../utils/tldraw/cc-base/cc-graph/cc-graph-styles';
import { logger } from '../../debugConfig';
import { NodeData } from '../../types/graph-shape';

export class NeoShapeService {
    private static readonly DATE_TIME_FIELDS = [
        'merged', 'created', 'start_date', 'end_date', 'start_time', 'end_time'
    ] as const;

    private static processDateTimeFields(data: Record<string, unknown>): Record<string, unknown> {
        const processed = { ...data };
        for (const key of Object.keys(processed)) {
            if (this.DATE_TIME_FIELDS.includes(key as typeof this.DATE_TIME_FIELDS[number]) && 
                processed[key] && 
                typeof processed[key] === 'object') {
                processed[key] = processed[key].toString();
            }
        }
        return processed;
    }

    static getShapeConfig(node: NavigationNode, nodeData: NodeData, centerX: number, centerY: number) {
        try {
            // Get the shape type based on the node type
            const shapeType = getShapeType(node.type as keyof CCNodeTypes);
            
            // Get theme colors based on the node type
            const theme = getThemeFromLabel(node.type);

            // Default dimensions
            const width = 500;
            const height = 350;

            // Process the node data - filter out properties that TLDraw doesn't expect
            const { cc_username, user_db_name, path, ...filteredNodeData } = nodeData;
            const processedProps = {
                ...this.processDateTimeFields(filteredNodeData),
                title: nodeData.title || node.label,
                w: width,
                h: height,
                state: {
                    parentId: null,
                    isPageChild: true,
                    hasChildren: null,
                    bindings: null
                },
                headerColor: theme.headerColor,
                backgroundColor: theme.backgroundColor,
                isLocked: false,
                __primarylabel__: node.type,
                uuid_string: node.id,
                node_storage_path: node.node_storage_path
            };

            // Add missing properties for cc-user-node
            if (shapeType === 'cc-user-node') {
                processedProps.user_id = node.id; // Use node.id as user_id
                processedProps.worker_node_data = JSON.stringify({
                    cc_username: nodeData.cc_username || '',
                    user_db_name: nodeData.user_db_name || ''
                });
            }

            logger.debug('neo-shape-service', '📄 Created shape configuration', {
                nodeId: node.id,
                shapeType,
                theme,
                props: processedProps
            });

            return {
                type: shapeType,
                x: centerX - (width / 2),
                y: centerY - (height / 2),
                props: processedProps
            };
        } catch (error) {
            logger.error('neo-shape-service', '❌ Failed to create shape configuration', {
                nodeId: node.id,
                error: error instanceof Error ? error.message : 'Unknown error'
            });
            throw error;
        }
    }
}

