package mysqlkindbox

import (
	"context"
	"database/sql"

	"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) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) {
	const op = "mysqlkindbox.GetKindBox"

	query := `SELECT * FROM kind_boxes WHERE id = ? AND deleted_at IS NULL`
	row := d.conn.Conn().QueryRowContext(ctx, query, kindBoxID)
	k, err := scanKindBox(row)
	if err != nil {
		if err == sql.ErrNoRows {
			return entity.KindBox{}, richerror.New(op).WithErr(err).
				WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
		}

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

	return k, nil
}
