package adminaddressaggregatorservice

import (
	"context"

	adminaddresshandler "git.gocasts.ir/ebhomengo/niki/delivery/http_server/admin/address"
	param "git.gocasts.ir/ebhomengo/niki/param/admin/address"
	admincityparam "git.gocasts.ir/ebhomengo/niki/param/admin/city"
	adminprovinceparam "git.gocasts.ir/ebhomengo/niki/param/admin/province"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) GetAggregatedByID(ctx context.Context, addressID uint) (adminaddresshandler.Address, error) {
	const op = "adminaddressaggregatorservice.GetAggregatedByID"

	address, err := s.repo.GetAddressWithProvinceAndCityByID(ctx, addressID)
	if err != nil {
		return adminaddresshandler.Address{}, richerror.New(op).WithErr(err)
	}
	return adminaddresshandler.Address{
		Address: param.Data{
			ID:           address.Address.ID,
			PostalCode:   address.Address.PostalCode,
			Address:      address.Address.Address,
			Name:         address.Address.Name,
			Lat:          address.Address.Lat,
			Lon:          address.Address.Lon,
			CityID:       address.Address.CityID,
			ProvinceID:   address.Address.ProvinceID,
			BenefactorID: address.Address.BenefactorID,
		},
		Province: adminprovinceparam.Data{
			ID:   address.Province.ID,
			Name: address.Province.Name,
		},
		City: admincityparam.Data{
			ID:         address.City.ID,
			Name:       address.City.Name,
			ProvinceId: address.City.ProvinceID,
		},
	}, nil
}
