from typing import Any, Dict, Tuple, List
from app.models.registry import MODEL_REGISTRY
from app.utils.hub import get_select_box_options


# Form layout service
def get_layout_service(
        type: str, model_name: str) -> Tuple[Dict[str, Any], List[int]]:
    if type not in ('add', 'copy', 'price'):
        return ({'error': 'Nepostojeći tip'}, [400])

    if model_name not in MODEL_REGISTRY:
        return ({'error': f'Model "{model_name}" nije registrovan'}, [400])

    if 'layout' not in MODEL_REGISTRY[model_name]:
        return ({'error': 'Ne postoji layout za dati model'}, [400])

    layout = MODEL_REGISTRY[model_name]['layout'][type]

    for device in ['desktop', 'mobile']:
        for row in layout[device]:
            for column in row['columns']:
                if column.get('method') == 'SelectBox':
                    response, status_codes = get_select_box_options(
                        model_name=model_name, type=type, column_name=column.get('data_name'))

                    if 'error' in response:
                        return (response, status_codes)
                    column['options'] = response

    return (layout, [200])
