package records

import (
	

	
	
)

type Reader[ any] struct {
	records []arrow.Record
}

func [ any]( ...arrow.Record) *Reader[] {
	var  
	 := reflect.TypeOf()
	for .Kind() == reflect.Ptr {
		 = .Elem()
	}
	if .Kind() != reflect.Struct {
		panic("frostdb/dynschema: " + .String() + " is not supported")
	}

	return &Reader[]{records: }
}

func ( *Reader[]) () int64 {
	var  int64
	for ,  := range .records {
		 += .NumRows()
	}
	return 
}

func ( *Reader[]) ( int)  {
	 := *new()
	 := reflect.TypeOf()

	// find the record with the value
	var  arrow.Record
	var  int64
	for ,  := range .records {
		if  < int(+.NumRows()) {
			 = 
			 =  - int()
			break
		}
		 += .NumRows()
	}

	for  := 0;  < .NumField(); ++ {
		 := .Field()
		,  := fieldName()

		 := .Schema().FieldIndices()
		if len() != 1 {
			panic("field " +  + " not found or ambiguous")
		}

		switch .Type.Kind() {
		case reflect.Bool:
			,  := .Column([0]).(*array.Boolean)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Float32:
			,  := .Column([0]).(*array.Float32)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Float64:
			,  := .Column([0]).(*array.Float64)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Int8:
			,  := .Column([0]).(*array.Int8)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Int16:
			,  := .Column([0]).(*array.Int16)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Int32:
			,  := .Column([0]).(*array.Int32)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Int64:
			,  := .Column([0]).(*array.Int64)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Uint8:
			,  := .Column([0]).(*array.Uint8)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Uint16:
			,  := .Column([0]).(*array.Uint16)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Uint32:
			,  := .Column([0]).(*array.Uint32)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.Uint64:
			,  := .Column([0]).(*array.Uint64)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		case reflect.String:
			// We probably need to support array.Binary too
			,  := .Column([0]).(*array.String)
			if ! || .IsNull() {
				continue
			}
			reflect.ValueOf(&).Elem().Field().Set(
				reflect.ValueOf(
					.Value(),
				),
			)
		default:
			panic("unsupported type " + .Type.String())
		}
	}

	return 
}