# Flyto2 Core - Source Available License
# Copyright (c) 2025 Flyto2. All Rights Reserved.
#
# This source code is licensed under the Flyto2 Source Available License v1.0.
# Commercial use requires a license. See LICENSE for details.

"""
Object Keys Module
Get all keys from an object
"""
from typing import Any, Dict

from ...registry import register_module
from ...schema import compose, presets
from ...errors import InvalidTypeError


@register_module(
    module_id='object.keys',
    version='1.0.0',
    category='data',
    subcategory='object',
    tags=['object', 'keys', 'dictionary'],
    label='Object Keys',
    label_key='modules.object.keys.label',
    description='Get all keys from an object',
    description_key='modules.object.keys.description',
    icon='Key',
    color='#F59E0B',

    # Connection types
    input_types=['object'],
    output_types=['array'],

    can_receive_from=['*'],
    can_connect_to=['*'],

    # Execution settings
    timeout_ms=5000,
    retryable=False,
    concurrent_safe=True,

    # Security settings
    requires_credentials=False,
    handles_sensitive_data=False,
    required_permissions=[],

    # Schema-driven params
    params_schema=compose(
        presets.INPUT_OBJECT(required=True),
    ),
    output_schema={
        'keys': {
            'type': 'array',
            'description': 'List of object keys'
        ,
                'description_key': 'modules.object.keys.output.keys.description'},
        'count': {
            'type': 'number',
            'description': 'Number of keys'
        ,
                'description_key': 'modules.object.keys.output.count.description'}
    },
    examples=[
        {
            'title': 'Get object keys',
            'params': {
                'object': {'name': 'John', 'age': 30, 'city': 'NYC'}
            }
        }
    ],
    author='Flyto Team',
    license='MIT'
)
async def object_keys(context: Dict[str, Any]) -> Dict[str, Any]:
    """Get all keys from an object."""
    params = context['params']
    obj = params.get('object')

    if not isinstance(obj, dict):
        raise InvalidTypeError(
            "object must be a dictionary",
            field="object",
            expected_type="dict",
            actual_type=type(obj).__name__
        )

    keys = list(obj.keys())

    return {
        'ok': True,
        'data': {
            'keys': keys,
            'count': len(keys)
        }
    }
