package common

import (
	"strings"

	"github.com/samber/lo"
)

func ParseErrorMessage(err error) string {
	// https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html
	mysqlLoginErrors := []string{
		"Error 1045", // Access denied for user
	}
	mysqlAccessErrors := []string{
		"Error 1044", // Access denied for user to database
		"Error 1142", // Command denied to user
		"Error 1143", // Command denied to user
		"Error 1227", // Access denied; you need (at least one of) the %s privilege(s) for this operation
		"Error 8121", // Privilege check fails
	}

	message := err.Error()
	if lo.ContainsBy(mysqlLoginErrors, func(e string) bool {
		return strings.Contains(message, e)
	}) {
		return "invalid user or password"
	}
	if lo.ContainsBy(mysqlAccessErrors, func(e string) bool {
		return strings.Contains(message, e)
	}) {
		return "no sufficient privilege"
	}

	return "internal error: task failed" // DO NOT RETURN FULL ERROR MESSAGE TO USER
}
