package mysqlkindbox

import (
	"database/sql"
	"time"

	"git.gocasts.ir/ebhomengo/niki/entity"
	"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)

func scanKindBox(scanner mysql.Scanner) (entity.KindBox, error) {
	var kindBox entity.KindBox
	var (
		kindBoxType       string
		amount            sql.NullInt64
		serialNumber      sql.NullString
		status            string
		senderAgentID     sql.NullInt64
		deliveredAt       sql.NullTime
		returnReferTimeID sql.NullInt64
		returnReferDate   sql.NullTime
		returnAddressID   sql.NullInt64
		receiverAgentID   sql.NullInt64
		returnedAt        sql.NullTime
		createdAt         time.Time
		updatedAt         time.Time
		deletedAt         sql.NullTime
	)

	err := scanner.Scan(
		&kindBox.ID,
		&kindBox.KindBoxReqID,
		&kindBox.BenefactorID,
		&kindBoxType,
		&amount,
		&serialNumber,
		&status,
		&kindBox.DeliverReferDate,
		&kindBox.DeliverAddressID,
		&kindBox.SenderAgentID,
		&kindBox.DeliveredAt,
		&returnReferTimeID,
		&returnReferDate,
		&returnAddressID,
		&receiverAgentID,
		&returnedAt,
		&createdAt,
		&updatedAt,
		&deletedAt,
	)
	if err != nil {
		return entity.KindBox{}, err
	}

	kindBox.KindBoxType = entity.MapToKindBoxType(kindBoxType)
	if amount.Valid {
		kindBox.Amount = uint(amount.Int64)
	}
	if serialNumber.Valid {
		kindBox.SerialNumber = serialNumber.String
	}
	kindBox.Status = entity.MapToKindBoxStatus(status)
	if senderAgentID.Valid {
		kindBox.SenderAgentID = uint(senderAgentID.Int64)
	}
	if deliveredAt.Valid {
		kindBox.DeliveredAt = deliveredAt.Time
	}
	if returnReferTimeID.Valid {
		kindBox.ReturnReferTimeID = uint(returnReferTimeID.Int64)
	}
	if returnReferDate.Valid {
		kindBox.ReturnReferDate = returnReferDate.Time
	}
	if returnAddressID.Valid {
		kindBox.ReturnAddressID = uint(returnAddressID.Int64)
	}
	if receiverAgentID.Valid {
		kindBox.ReceiverAgentID = uint(receiverAgentID.Int64)
	}
	if returnedAt.Valid {
		kindBox.ReturnedAt = returnedAt.Time
	}

	return kindBox, nil
}
