"""LookML Generator implementations."""

import os
from typing import Dict
from looker_loader.models.looker import LookerView, ValidatedLookerDimension, LookerMeasure
import logging


class LookmlGenerator:
    """
        Main LookML generator that coordinates dimension, view, and explore generation.
    """
    def __init__(self, cli_args):
        self._cli_args = cli_args
    
    def _generate_views(self, model, views = None, parent = None):
        """Split up the model into views"""

        if views is None:
            views = []

        view_dimensions = []
        view_measures = []

        if parent is not None:
            view_name = f"{parent}__{model.name}"
        else:
            view_name = model.name

        for field in model.fields:
            view_dimensions.append(field.model_dump())

            if field.fields is not None:
                self._generate_views(field, views, parent=view_name)
            if field.measures is not None:
                for measure in field.measures:
                    view_measures.append(measure.model_dump())
        view = LookerView(**{
            "name": view_name,
            "sql_table_name": model.sql_table_name,
            "dimensions": view_dimensions,
            "measures": view_measures,
        })
        views.append(view)

        return views

    def _generate_joins(self, model, joins = None, parent = None, depth = 0):
        """Recursively create joins for a model by detecting nested fields."""
        if joins is None:
            joins = []

        if parent is not None:
            join_name = f"{parent}__{model.name}"
        else:
            join_name = model.name

        for field in model.fields:
            if field.fields is not None:
                self._generate_joins(field, joins, parent=join_name, depth=depth + 1)

        if parent is not None:
            join = {
                "name": join_name,
                "sql": f"LEFT JOIN UNNEST(${{{parent}.{model.name}}}) AS {join_name}",
                "type": "left_outer",
                "relationship": "one_to_many",
                "required_joins": parent if depth > 1 else None,
            }
            joins.append(join)

        return joins

    def _create_explore(self, model):
        """Create an explore for the model"""
        joins = self._generate_joins(model)

        if joins:
            explore = {
                "name": model.name,
                "joins": joins,
            }
            return explore

    def generate(self, model) -> Dict:
        """Generate LookML for a model."""
        
        view_groups = self._generate_views(model)

        explore = self._create_explore(model)

        return view_groups, explore