package mysqladdress

import (
	"context"
	"database/sql"
	"errors"

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

// TODO - use this approach or return (bool,entity.Address,error).
func (d *DB) GetAddressByID(ctx context.Context, id uint) (entity.Address, error) {
	const op = "mysqladdress.IsExistAddressByID"

	query := `select * from addresses where id = ? and deleted_at is null`
	//nolint
	stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyAddressGetByID, query)
	if err != nil {
		return entity.Address{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
	}

	row := stmt.QueryRowContext(ctx, id)
	address, err := scanAddress(row)
	if err != nil {
		sErr := sql.ErrNoRows
		//TODO-errorsas: second argument to errors.As should not be *error
		//nolint
		if errors.As(err, &sErr) {
			return entity.Address{}, nil
		}

		// TODO - log unexpected error for better observability
		return entity.Address{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return address, nil
}

func (d *DB) GetAddress(ctx context.Context, addressID, benefactorID uint) (entity.Address, error) {
	const op = "mysqladdress.GetAddress"

	row := d.conn.Conn().QueryRowContext(ctx, `select * from addresses where id = ? and benefactor_id = ? and deleted_at is null`, addressID, benefactorID)

	address, err := scanAddress(row)
	if err != nil {
		sErr := sql.ErrNoRows
		//TODO-errorsas: second argument to errors.As should not be *error
		//nolint
		if errors.As(err, &sErr) {
			return entity.Address{}, richerror.New(op).WithMessage(errmsg.ErrorMsgNotFound).
				WithKind(richerror.KindNotFound)
		}

		// TODO - log unexpected error for better observability
		return entity.Address{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return address, nil
}

func (d *DB) GetAddressWithProvinceAndCityByID(ctx context.Context, addressID uint) (entity.AddressAggregated, error) {
	const op = "mysqladdress.GetAddressWithProvinceAndCityByID"

	query := `select * from addresses join cities on city_id = cities.id join provinces on cities.province_id = provinces.id where addresses.id = ? and deleted_at is null`
	row := d.conn.Conn().QueryRowContext(ctx, query, addressID)

	aggregateAddress, err := scanAddressAggregated(row)
	if err != nil {
		sErr := sql.ErrNoRows
		//nolint
		if errors.As(err, &sErr) {
			return entity.AddressAggregated{}, richerror.New(op).WithMessage(errmsg.ErrorMsgNotFound).
				WithKind(richerror.KindNotFound)
		}

		return entity.AddressAggregated{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return aggregateAddress, nil
}
