package repository

import (
	"errors"
	"fmt"
	"go-nkode/internal/entities"
	"go-nkode/internal/models"
)

type InMemoryDb struct {
	Customers map[models.CustomerID]entities.Customer
	Users     map[models.UserId]entities.User
	userIdMap map[string]models.UserId
}

func NewInMemoryDb() InMemoryDb {
	return InMemoryDb{
		Customers: make(map[models.CustomerID]entities.Customer),
		Users:     make(map[models.UserId]entities.User),
		userIdMap: make(map[string]models.UserId),
	}
}

func (db *InMemoryDb) GetCustomer(id models.CustomerID) (*entities.Customer, error) {
	customer, exists := db.Customers[id]
	if !exists {
		return nil, errors.New(fmt.Sprintf("customer %s dne", customer.ID))
	}
	return &customer, nil
}

func (db *InMemoryDb) GetUser(username models.UserEmail, customerId models.CustomerID) (*entities.User, error) {
	key := userIdKey(customerId, username)
	userId, exists := db.userIdMap[key]
	if !exists {
		return nil, errors.New(fmt.Sprintf("customer %s with username %s dne", customerId, username))
	}
	user, exists := db.Users[userId]
	if !exists {
		panic(fmt.Sprintf("userId %s with customerId %s and username %s with no user", userId, customerId, username))
	}
	return &user, nil
}

func (db *InMemoryDb) CreateCustomer(customer entities.Customer) error {
	_, exists := db.Customers[customer.ID]

	if exists {
		return errors.New(fmt.Sprintf("can write customer %s; already exists", customer.ID))
	}
	db.Customers[customer.ID] = customer
	return nil
}

func (db *InMemoryDb) WriteNewUser(user entities.User) error {
	_, exists := db.Customers[user.CustomerId]
	if !exists {
		return errors.New(fmt.Sprintf("can't add user %s to customer %s: customer dne", user.Email, user.CustomerId))
	}
	userExists, _ := db.GetUser(user.Email, user.CustomerId)

	if userExists != nil {
		return errors.New(fmt.Sprintf("can't write new user %s, alread exists", user.Email))
	}
	key := userIdKey(user.CustomerId, user.Email)
	db.userIdMap[key] = user.Id
	db.Users[user.Id] = user
	return nil
}

func (db *InMemoryDb) UpdateUserNKode(user entities.User) error {
	return errors.ErrUnsupported
}

func (db *InMemoryDb) UpdateUserInterface(userId models.UserId, ui entities.UserInterface) error {
	user, exists := db.Users[userId]
	if !exists {
		return errors.New(fmt.Sprintf("can't update user %s, dne", user.Id))
	}
	user.Interface = ui
	db.Users[userId] = user
	return nil
}

func (db *InMemoryDb) UpdateUserRefreshToken(userId models.UserId, refreshToken string) error {
	return nil
}

func (db *InMemoryDb) Renew(id models.CustomerID) error {
	customer, exists := db.Customers[id]
	if !exists {
		return errors.New(fmt.Sprintf("customer %s does not exist", id))
	}
	setXor, attrsXor, err := customer.RenewKeys()
	if err != nil {
		return err
	}
	db.Customers[id] = customer
	for _, user := range db.Users {
		if user.CustomerId == id {
			err = user.RenewKeys(setXor, attrsXor)
			if err != nil {
				return err
			}
			db.Users[user.Id] = user
		}
	}
	return nil
}

func (db *InMemoryDb) RefreshUserPasscode(user entities.User, passocode []int, customerAttr entities.CustomerAttributes) error {
	err := user.RefreshPasscode(passocode, customerAttr)
	if err != nil {
		return err
	}
	db.Users[user.Id] = user
	return nil
}

func (db *InMemoryDb) RandomSvgInterface(kp entities.KeypadDimension) ([]string, error) {
	return make([]string, kp.TotalAttrs()), nil
}

func (db *InMemoryDb) RandomSvgIdxInterface(kp entities.KeypadDimension) (models.SvgIdInterface, error) {
	svgs := make(models.SvgIdInterface, kp.TotalAttrs())
	for idx := range svgs {
		svgs[idx] = idx
	}
	return svgs, nil
}

func (db *InMemoryDb) GetSvgStringInterface(idxs models.SvgIdInterface) ([]string, error) {
	return make([]string, len(idxs)), nil
}

func userIdKey(customerId models.CustomerID, username models.UserEmail) string {
	key := fmt.Sprintf("%s:%s", customerId, username)
	return key
}
