package core

import (
	"context"
	"errors"
	"os"
	"strings"
	"time"

	"gitea.mrixs.me/minecraft-platform/backend/internal/database"
	"gitea.mrixs.me/minecraft-platform/backend/internal/models"
	"github.com/golang-jwt/jwt/v5"
	"github.com/google/uuid"
	"golang.org/x/crypto/bcrypt"
)

var (
	ErrInvalidCredentials = errors.New("invalid credentials")
)

type AuthService struct {
	UserRepo *database.UserRepository
}

// Authenticate проверяет учетные данные и возвращает данные для ответа Yggdrasil.
func (s *AuthService) Authenticate(ctx context.Context, req models.AuthenticateRequest) (*models.AuthenticateResponse, error) {
	user, err := s.UserRepo.GetUserByUsername(ctx, req.Username)
	if err != nil {
		if errors.Is(err, database.ErrUserNotFound) {
			return nil, ErrInvalidCredentials
		}
		return nil, err
	}

	err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(req.Password))
	if err != nil {
		return nil, ErrInvalidCredentials
	}

	accessToken := uuid.New().String()

	err = s.UserRepo.CreateAccessToken(ctx, user.ID, accessToken, req.ClientToken)
	if err != nil {
		return nil, err
	}

	profile := models.ProfileInfo{
		ID:   strings.ReplaceAll(user.UUID.String(), "-", ""),
		Name: user.Username,
	}

	response := &models.AuthenticateResponse{
		AccessToken:       accessToken,
		ClientToken:       req.ClientToken,
		AvailableProfiles: []models.ProfileInfo{profile},
		SelectedProfile:   profile,
		User: &models.UserProperty{
			ID:         user.UUID.String(),
			Properties: []any{},
		},
	}

	return response, nil
}

// LoginUser проверяет учетные данные и генерирует JWT для веб-сессии.
func (s *AuthService) LoginUser(ctx context.Context, req models.LoginRequest) (string, *models.User, error) {
	user, err := s.UserRepo.GetUserByLogin(ctx, req.Login)
	if err != nil {
		if errors.Is(err, database.ErrUserNotFound) {
			return "", nil, ErrInvalidCredentials
		}
		return "", nil, err
	}

	err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(req.Password))
	if err != nil {
		return "", nil, ErrInvalidCredentials
	}

	expirationTime := time.Now().Add(72 * time.Hour)

	claims := &jwt.MapClaims{
		"exp":     expirationTime.Unix(),
		"iat":     time.Now().Unix(),
		"user_id": user.ID,
		"role":    user.Role,
	}

	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)

	jwtSecret := os.Getenv("JWT_SECRET_KEY")
	tokenString, err := token.SignedString([]byte(jwtSecret))
	if err != nil {
		return "", nil, err
	}

	user.PasswordHash = ""

	return tokenString, user, nil
}
