package mysqladdress

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (d *DB) CreateBenefactorAddress(ctx context.Context, address entity.Address) (entity.Address, error) {
	const op = "mysqlbenefactor.createBenefactorAddress"

	res, err := d.conn.Conn().ExecContext(ctx, `insert into addresses(postal_code, address, lat, lon,province_id,city_id,benefactor_id) values(?, ?, ?,?,?,?,?)`,
		address.PostalCode, address.Address, address.Lat, address.Lon, address.ProvinceID, address.CityID, address.BenefactorID)
	if err != nil {
		return entity.Address{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
	}

	//nolint
	// error is always nil
	id, _ := res.LastInsertId()
	address.ID = uint(id)

	return address, nil
}
