package mysqlkindboxreq

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) GetAllKindBoxReq(ctx context.Context) ([]entity.KindBoxReq, error) {
	const op = "mysqlkindboxreq.GetAllKindBoxReq"

	// TODO - add sort and filter
	rows, err := d.conn.Conn().QueryContext(ctx, "select * from kind_box_reqs")
	if err != nil {
		return nil,
			richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithErr(err).WithKind(richerror.KindUnexpected)
	}

	defer rows.Close()

	// An album slice to hold data from returned rows.
	var kindBoxReqs []entity.KindBoxReq

	// Loop through rows, using Scan to assign column data to struct fields.
	for rows.Next() {
		kindBoxReq, sErr := scanKindBoxReq(rows)
		if sErr != nil {
			return nil, richerror.New(op).WithErr(sErr).
				WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected)
		}
		kindBoxReqs = append(kindBoxReqs, kindBoxReq)
	}

	if rErr := rows.Err(); rErr != nil {
		return nil, richerror.New(op).WithErr(rErr).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return kindBoxReqs, nil
}
