# frozen_string_literal: true

# This file is autogenerated by Deimos, Do NOT modify
module Schemas; module MyNamespace
  ### Primary Schema Class ###
  # Autogenerated Schema for Record at com.my-namespace.MySchemaWithUnionType
  class MySchemaWithUnionType < Deimos::SchemaClass::Record

    ### Secondary Schema Classes ###
    # Autogenerated Schema for Record at com.flipp.content.Record1
    class Record1 < Deimos::SchemaClass::Record
  
      ### Attribute Accessors ###
      # @return [Hash<String, Integer>]
      attr_accessor :record1_map
      # @return [Integer]
      attr_accessor :record1_id
  
      # @override
      def initialize(_from_message: false, record1_map: {},
                     record1_id: 0)
        @_from_message = _from_message
        super
        self.record1_map = record1_map
        self.record1_id = record1_id
      end
  
      # @override
      def schema
        'Record1'
      end
  
      # @override
      def namespace
        'com.flipp.content'
      end
  
      # @override
      def as_json(_opts={})
        {
          'record1_map' => @record1_map,
          'record1_id' => @record1_id
        }
      end
    end

    # Autogenerated Schema for Record at com.flipp.content.Record2
    class Record2 < Deimos::SchemaClass::Record
  
      ### Attribute Accessors ###
      # @return [String]
      attr_accessor :record2_id
  
      # @override
      def initialize(_from_message: false, record2_id: "")
        @_from_message = _from_message
        super
        self.record2_id = record2_id
      end
  
      # @override
      def schema
        'Record2'
      end
  
      # @override
      def namespace
        'com.flipp.content'
      end
  
      # @override
      def as_json(_opts={})
        {
          'record2_id' => @record2_id
        }
      end
    end

    # Autogenerated Schema for Record at com.flipp.content.Record3
    class Record3 < Deimos::SchemaClass::Record
  
      ### Attribute Accessors ###
      # @return [Float]
      attr_accessor :record3_id
  
      # @override
      def initialize(_from_message: false, record3_id: 0.0)
        @_from_message = _from_message
        super
        self.record3_id = record3_id
      end
  
      # @override
      def schema
        'Record3'
      end
  
      # @override
      def namespace
        'com.flipp.content'
      end
  
      # @override
      def as_json(_opts={})
        {
          'record3_id' => @record3_id
        }
      end
    end

    # Autogenerated Schema for Record at com.flipp.content.Record4
    class Record4 < Deimos::SchemaClass::Record
  
      ### Attribute Accessors ###
      # @return [Integer]
      attr_accessor :record4_id
  
      # @override
      def initialize(_from_message: false, record4_id: 0)
        @_from_message = _from_message
        super
        self.record4_id = record4_id
      end
  
      # @override
      def schema
        'Record4'
      end
  
      # @override
      def namespace
        'com.flipp.content'
      end
  
      # @override
      def as_json(_opts={})
        {
          'record4_id' => @record4_id
        }
      end
    end


    ### Attribute Readers ###
    # @return [nil, Record1, Record2, Record3, Record4, Integer, Array<String>]
    attr_reader :test_union_type

    ### Attribute Accessors ###
    # @return [String]
    attr_accessor :test_id
    # @return [nil, Integer]
    attr_accessor :test_long

    ### Attribute Writers ###
    # @return [nil, Record1, Record2, Record3, Record4, Integer, Array<String>]
    def test_union_type=(value)
      @test_union_type = initialize_test_union_type_type(value, from_message: self._from_message)
    end

    # Helper method to determine which schema type to use for test_union_type
    # @param value [Hash, nil]
    # @param from_message [Boolean] whether this was initialized from a real Avro message
    # @return [Object, nil]
    def initialize_test_union_type_type(value, from_message: false)
      return nil if value.nil?

      klass = [Record1, Record2, Record3, Record4].find do |candidate|
        fields = candidate.new.as_json.keys
        (value.keys - fields).empty?
      end

      klass.initialize_from_value(value, from_message: self._from_message)
    end

    # @override
    def initialize(_from_message: false, test_id: "",
                   test_long: nil,
                   test_union_type: nil)
      @_from_message = _from_message
      super
      self.test_id = test_id
      self.test_long = test_long
      self.test_union_type = test_union_type
    end

    # @override
    def schema
      'MySchemaWithUnionType'
    end

    # @override
    def namespace
      'com.my-namespace'
    end

    # @override
    def as_json(_opts={})
      {
        'test_id' => @test_id,
        'test_long' => @test_long,
        'test_union_type' => @test_union_type&.as_json
      }
    end
  end
end; end
