//
//  PartialDomainModels.stencil
//  Sourcery template for generating partial domain models
//
//  Usage: Annotate your domain struct with `// sourcery: generable`
//

import Foundation
import InferenceKit

// MARK: - Partial Domain Models (Auto-generated for streaming)
{% for type in types.structs where type.annotations.generable %}

/// Partial `{{ type.name }}` for streaming incomplete data
public struct Partial{{ type.name }}: Sendable {
{% for variable in type.storedVariables %}
    public let {{ variable.name }}: {% if variable.isOptional -%}
        {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.array.elementTypeName.name -%}
            [Partial{{ t.name }}]
        {%- empty -%}
            {%- for t in types.all where t.annotations.generable and t.name == variable.unwrappedTypeName -%}
                Partial{{ variable.unwrappedTypeName }}
            {%- empty -%}
                {{ variable.typeName }}
            {%- endfor -%}
        {%- endfor -%}
    {%- else -%}
        {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.array.elementTypeName.name -%}
            [Partial{{ t.name }}]?
        {%- empty -%}
            {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.name -%}
                Partial{{ variable.typeName.name }}?
            {%- empty -%}
                {{ variable.typeName }}?
            {%- endfor -%}
        {%- endfor -%}
    {%- endif %}
{% endfor %}
    public let generationID: String?
    
    public init(
        {%- for variable in type.storedVariables %}
        {{ variable.name }}: {% if variable.isOptional -%}
            {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.array.elementTypeName.name -%}
                [Partial{{ t.name }}]
            {%- empty -%}
                {%- for t in types.all where t.annotations.generable and t.name == variable.unwrappedTypeName -%}
                    Partial{{ variable.unwrappedTypeName }}
                {%- empty -%}
                    {{ variable.typeName }}
                {%- endfor -%}
            {%- endfor -%}
        {%- else -%}
            {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.array.elementTypeName.name -%}
                [Partial{{ t.name }}]?
            {%- empty -%}
                {%- for t in types.all where t.annotations.generable and t.name == variable.typeName.name -%}
                    Partial{{ variable.typeName.name }}?
                {%- empty -%}
                    {{ variable.typeName }}?
                {%- endfor -%}
            {%- endfor -%}
        {%- endif %} = nil,
        {% endfor %}
        generationID: String? = nil
    ) {
{% for variable in type.storedVariables %}
        self.{{ variable.name }} = {{ variable.name }}
{% endfor %}
        self.generationID = generationID
    }
}
{% endfor %}
