import enum

from sqlalchemy import Column, Integer, String, Float, Numeric
from sqlalchemy.types import Enum

from .database import Base

# Required imports from other modules (TODO: Modules required for foreign keys)
{% for cls in package.classes %}{% if cls.generalization != None %}{% if cls.generalization.package != package %}
from {{ cls.generalization.package.name | case_package }}.models import {{ cls.generalization.name | case_class }}{% endif %}{% endif %}{% endfor %}

# Enumerations
{% for enum in package.enumerations %}class {{ enum.name | case_class }}(enum.Enum):{% for attr in enum.values %}
    {{ attr | case_attribute | replace('-', '_') | replace('/', '_') }} = '{{ attr }}'{% endfor %}


{% endfor %}

# Abstract Classes
{% for cls in package.classes %}{% if cls.is_abstract %}class {{ cls.name | case_class }}( Base ):
    __abstract__ = True
{% for attr in cls.attributes %}
{% if attr.classification %}    {% if attr.alias %}{{ attr.alias | case_attribute }}{% else %}{{ attr.name | case_attribute }}{% endif %} = Column( Enum({{ attr.classification.name | case_class }}) )
{% else %}    {% if attr.alias %}{{ attr.alias | case_attribute }}{% else %}{{ attr.name | case_attribute }}{% endif %} = Column( {{ attr.dest_type }}{% if attr.length %}(length={{attr.length}}){% endif %}{% if attr.is_id %}, primary_key=True, index=True{% endif %} {% if attr.scale %}{{attr.precision, attr.scale}}{% endif %} ){% endif %}{% endfor %}


{% endif %}{% endfor %}

# Concrete Classes (TODO: Do inherited classes first)
{% for cls in package.classes %}{% if not cls.is_abstract %}class {{ cls.name | case_class }}{% if not cls.generalization %}( Base ):{% else %}( {{ cls.generalization.name | case_class }} ):{% endif %}
    __tablename__ = "{% if cls.alias %}{{ cls.alias | case_class }}{% else %}{{ cls.name | snakecase | uppercase }}{% endif %}" 
{% for attr in cls.attributes %}
{% if attr.classification %}    {% if attr.alias %}{{ attr.alias | case_attribute }}{% else %}{{ attr.name | case_attribute }}{% endif %} = Column( Enum({{ attr.classification.name | case_class }}))
{% else %}    {% if attr.alias %}{{ attr.alias | case_attribute }}{% else %}{{ attr.name | case_attribute }}{% endif %} = Column( {{ attr.dest_type }}{% if attr.length %}(length={{attr.length}}){% endif %}{% if attr.is_id %}, primary_key=True, index=True{% endif %}{% if attr.scale %}{{attr.precision, attr.scale}}{% endif %} ){% endif %}{% endfor %}


{% endif %}{% endfor %}
