package apperrorsx

import (
	"errors"
	"testing"

	mysqldriver "github.com/go-sql-driver/mysql"
	"gorm.io/gorm"

	"wucher/internal/service"
)

func TestTranslate_AuthPIN(t *testing.T) {
	got := Translate(ModulePIN, service.ErrInvalidSecurityPIN)
	if got == nil || got.Code != CodePINIncorrect {
		t.Fatalf("expected %s, got %#v", CodePINIncorrect, got)
	}

	got = Translate(ModuleAuth, service.ErrInvalidToken)
	if got == nil || got.Code != CodeTokenInvalid {
		t.Fatalf("expected %s, got %#v", CodeTokenInvalid, got)
	}
}

func TestTranslate_HelicopterAndFallback(t *testing.T) {
	got := Translate(ModuleHelicopter, gorm.ErrRecordNotFound)
	if got == nil || got.Code != CodeHelicopterNotFound {
		t.Fatalf("expected %s, got %#v", CodeHelicopterNotFound, got)
	}

	got = Translate(ModuleHelicopter, &mysqldriver.MySQLError{Number: 1062, Message: "duplicate"})
	if got == nil || got.Code != CodeHelicopterDuplicate {
		t.Fatalf("expected %s, got %#v", CodeHelicopterDuplicate, got)
	}

	got = Translate("99", errors.New("unknown issue"))
	if got == nil || got.Code != CodeInternalServerError {
		t.Fatalf("expected %s, got %#v", CodeInternalServerError, got)
	}
}
