package mysqladmin

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) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) {
	const op = "mysqladmin.AddAdmin"

	res, err := d.conn.Conn().ExecContext(ctx, `insert into admins(first_name,last_name,password,phone_number,
                   role,description,email,gender,status) values (?,?,?,?,?,?,?,?,?)`,
		admin.FirstName, admin.LastName, admin.Password, admin.PhoneNumber, admin.Role.String(), admin.Description, admin.Email,
		admin.Gender.String(), admin.Status.String())
	if err != nil {
		return entity.Admin{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
	}

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

	return admin, nil
}
