package my_golang_fuzz

import (
	"bytes"

	"linux-auth/internal/auth"
	"linux-auth/internal/db"
)

func Fuzz(data []byte) int {
	if err := db.Init("test_data.db"); err != nil {
		return -1
	}
	defer db.Close()

	parts := bytes.SplitN(data, []byte{0}, 2)
	if len(parts) != 2 {
		return 0
	}

	username := string(parts[0])
	password := string(parts[1])

	ok, err := auth.Authenticate(username, password)

	if err != nil {
		return -1
	}

	if ok {
		if (username == "admin" && password == "admin123") ||
			(username == "user1" && password == "password1") {
			return 1
		}
		return -1
	}

	return 1
}
