package mysqlkindboxreq

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) GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) {
	op := richerror.Op("mysqlkindboxreq.GetByID")
	row := d.conn.Conn().QueryRowContext(ctx, `select * from kind_box_reqs where id = ? and deleted_at is null`, id)
	k, err := scanKindBoxReq(row)
	if err != nil {
		return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return k, nil
}

func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error) {
	op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
	row := d.conn.Conn().QueryRowContext(ctx,
		"select * from kind_box_reqs where id = ? and deleted_at is null", kindBoxReqID)
	k, err := scanKindBoxReq(row)
	if err != nil {
		if err == sql.ErrNoRows {
			return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
				WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
		}
		return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
	}

	return k, nil
}
