from typing import Optional, List
from datetime import datetime
from sqlmodel import SQLModel
from app.models.{{ model.module_name }} import {{ model.name }}

# Shared properties
class {{ model.name }}Base(SQLModel):
{% set ns = namespace(has_field=false) %}
{% for field in model.fields %}
    {% if 'r' in field.permissions and field.name != 'id' %}
    {{ field.name }}: {{ field.type }}
    {% set ns.has_field = true %}
    {% endif %}
{% endfor %}
{% if not ns.has_field %}
    pass
{% endif %}


# Properties to receive via API on creation
class {{ model.name }}Create(SQLModel):
{% set ns = namespace(has_field=false) %}
{% for field in model.fields %}
    {% if 'c' in field.permissions and field.name != 'id' %}
    {{ field.name }}: {{ field.type }}
    {% set ns.has_field = true %}
    {% endif %}
{% endfor %}
{% if model.m2m_fields %}
    {% for m2m in model.m2m_fields %}
    {{ m2m.name }}: Optional[List[int]] = []
    {% set ns.has_field = true %}
    {% endfor %}
{% endif %}
{% if not ns.has_field %}
    pass
{% endif %}

# Properties to receive via API on update
class {{ model.name }}Update(SQLModel):
{% set ns = namespace(has_field=false) %}
{% for field in model.fields %}
    {% if 'u' in field.permissions and field.name != 'id' %}
    {{ field.name }}: {{ field.type }} = None
    {% set ns.has_field = true %}
    {% endif %}
{% endfor %}
{% if model.m2m_fields %}
    {% for m2m in model.m2m_fields %}
    {{ m2m.name }}: Optional[List[int]] = None
    {% set ns.has_field = true %}
    {% endfor %}
{% endif %}

{% if not ns.has_field %}
    pass
{% endif %}

class {{ model.name }}Read({{ model.name }}Base):
    id: int
{% if model.m2m_fields %}
    # For now, we don't return m2m fields in Read schema by default to avoid N+1 queries
    # Frontend will fetch them separately if needed, or we can add them later
    pass
{% endif %}
