package mysqlkindboxreq

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"
)

func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) {
	const op = "mysqlkindboxreq.AddKindBoxReq"

	res, err := d.conn.Conn().ExecContext(ctx, `insert into kind_box_reqs(benefactor_id,kind_box_type,deliver_address_id,count_requested,deliver_refer_date,status) values (?,?,?,?,?,?)`,
		kindBoxReq.BenefactorID, kindBoxReq.KindBoxType.String(), kindBoxReq.DeliverAddressID, kindBoxReq.CountRequested, kindBoxReq.DeliverReferDate, kindBoxReq.Status.String())
	if err != nil {
		return entity.KindBoxReq{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
	}

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

	return kindBoxReq, nil
}

func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
	op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
	_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set count_accepted = ? , status = ? where id = ?`,
		countAccepted, entity.KindBoxReqAcceptedStatus.String(), kindBoxReqID)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}

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 = ?`, 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) KindBoxRequestExist(id uint) (bool, error) {
	op := richerror.Op("mysqlkindboxreq.KindBoxRequestExist")
	row := d.conn.Conn().QueryRow(`select * from kind_box_reqs where id = ?`, id)
	_, sErr := scanKindBoxReq(row)
	if sErr != nil {
		if errors.Is(sErr, sql.ErrNoRows) {
			return false, nil
		}

		return false, richerror.New(op).WithErr(sErr).WithMessage(errmsg.ErrorMsgCantScanQueryResult).
			WithKind(richerror.KindUnexpected).WithMeta(map[string]any{"id": id})
	}

	return true, nil
}

func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
	op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
	_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set description = ? , status = ? where id = ?`,
		description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}

func (d DB) RollbackKindBoxRequestStatus(ctx context.Context, id uint) error {
	op := richerror.Op("mysqlkindboxreq.RollbackKindBoxRequestStatus")
	_, err := d.conn.Conn().ExecContext(ctx, `update kind_box_reqs set status = ? where id = ?`, entity.KindBoxReqPendingStatus.String(), id)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
	}

	return nil
}

func (d DB) GetKindBoxReqByID(ctx context.Context, kindBoxReqID, benefactorID uint) (entity.KindBoxReq, error) {
	op := richerror.Op("mysqlkindboxreq.GetKindBoxReqByID")
	row := d.conn.Conn().QueryRowContext(ctx,
		"select kind_box_reqs.* from kind_box_reqs where kind_box_reqs.id = ? and kind_box_reqs.benefactor_id = ?", kindBoxReqID, benefactorID,
	)
	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
}
