package db

import (
	"encoding/json"
	"errors"
	"fmt"
	"log"
	"os"

	// "github.com/alexedwards/argon2id"
)

type User struct {
    Email string `json:"email"`
	Password string `json:"password"`
    // Other fields as needed
}

var DB map[string]User

type GetService struct{}
var Get = GetService{}

func (g GetService) UserByEmail(email string) (map[string]interface{}, error) {
	for key, value := range DB {
        if value.Email == email {
            log.Println("found")
            return map[string]interface{}{
                "key":   key,
                "email": value.Email,
				"password": value.Password,
            }, nil
        }
        fmt.Printf("Key: %s, Value: %v\n", key, value)
    }

	return nil, errors.New("user not found")
}

func InitDB() error {
	file, err := os.Open("../db/users.json")
	if err != nil {
		fmt.Println("Error opening file:", err)
		return errors.New("Failed to read db")
	}
	defer file.Close()

	var result map[string]User
    err = json.NewDecoder(file).Decode(&result)
    if err != nil {
        fmt.Println("Error decoding JSON:", err)
        return errors.New("failed to decode db")
    }

	DB = result

	return nil
}