package development

import (
	"context"
	"git.markbailey.dev/cervers/ptpp/lib/database/dto"
	pterror "git.markbailey.dev/cervers/ptpp/lib/error"
	"git.markbailey.dev/cervers/ptpp/models/sqlite"
)

type UserRepository struct{}

func (ur UserRepository) CreateUser(u dto.User) (*dto.User, error) {
	ctx := context.Background()
	user, err := TestQueries.CreateUser(ctx, sqlite.CreateUserParams{
		Username:     u.Username,
		Password:     u.Password,
		Name:         u.Name,
		Email:        u.Email,
		AuthToken:    u.AuthToken,
		Authorized:   u.Authorized,
		Admin:        u.Admin,
		Organization: u.Organization,
	})
	if err != nil {
		return nil, pterror.Wrap(err, "Error creating user")
	}

	return &dto.User{
		Identifier:   user.Identifier,
		Username:     user.Username,
		Password:     user.Password,
		Name:         user.Name,
		Email:        user.Email,
		AuthToken:    user.AuthToken,
		Authorized:   user.Authorized,
		Admin:        user.Admin,
		Organization: user.Organization,
	}, nil
}

func (ur UserRepository) FindUserByUsername(username string) (*dto.User, error) {
	ctx := context.Background()
	user, err := TestQueries.FindUserByUsername(ctx, username)
	if err != nil {
		return nil, pterror.Wrap(err, "Error finding user by username")
	}

	return &dto.User{
		Identifier:   user.Identifier,
		Username:     user.Username,
		Password:     user.Password,
		Name:         user.Name,
		Email:        user.Email,
		AuthToken:    user.AuthToken,
		Authorized:   user.Authorized,
		Admin:        user.Admin,
		Organization: user.Organization,
	}, nil
}
