package mysqladdress

import (
	"database/sql"
	"time"

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

func scanProvince(scanner mysql.Scanner) (entity.Province, error) {
	var createdAt, updatedAt time.Time
	var province entity.Province

	err := scanner.Scan(&province.ID, &province.Name, &createdAt, &updatedAt)

	return province, err
}

func scanCity(scanner mysql.Scanner) (entity.City, error) {
	var createdAt, updatedAt time.Time
	var city entity.City

	err := scanner.Scan(&city.ID, &city.Name, &city.ProvinceID, &createdAt, &updatedAt)

	return city, err
}

func scanAddress(scanner mysql.Scanner) (entity.Address, error) {
	var createdAt, updatedAt time.Time
	var deletedAt sql.NullTime
	var address entity.Address

	err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon,
		&address.Name, &address.CityID, &address.ProvinceID, &address.BenefactorID,
		&createdAt, &updatedAt, &deletedAt)

	return address, err
}

func scanAddressAggregated(scanner mysql.Scanner) (entity.AddressAggregated, error) {
	var createdAt, updatedAt time.Time
	var deletedAt sql.NullTime
	var address entity.Address
	var province entity.Province
	var city entity.City

	err := scanner.Scan(&address.ID, &address.PostalCode, &address.Address, &address.Lat, &address.Lon,
		&address.Name, &address.CityID, &address.ProvinceID, &address.BenefactorID,
		&createdAt, &updatedAt, &deletedAt,
		&city.ID, &city.Name, &city.ProvinceID, &createdAt, &updatedAt,
		&province.ID, &province.Name, &createdAt, &updatedAt)
	return entity.AddressAggregated{
		Address:  address,
		City:     city,
		Province: province,
	}, err
}
