package mysqlbenefactor

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"
	"git.gocasts.ir/ebhomengo/niki/repository/mysql"
)

func (d *DB) UpdateGeneralBenefactor(ctx context.Context, benefactor entity.Benefactor) error {
	const op = "mysqlbenefactor.UpdateGeneralBenefactor"

	query := `UPDATE benefactors
			  SET first_name = ?, last_name = ?, birth_date = ?
			  WHERE id = ?`
	//nolint
	stmt, err := d.conn.PrepareStatement(ctx, mysql.StatementKeyBenefactorUpdate, query)
	if err != nil {
		return richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgCantPrepareStatement).WithKind(richerror.KindUnexpected)
	}
	_, uErr := stmt.ExecContext(ctx, benefactor.FirstName,
		benefactor.LastName, benefactor.BirthDate, benefactor.ID)

	if uErr != nil {
		return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord).
			WithKind(richerror.KindUnexpected)
	}

	return nil
}
