package security

import (
	"fmt"
	"time"

	"github.com/golang-jwt/jwt/v5"
)

// JWTService interface defines the contract for JWT operations
type JWTService interface {
	GenerateToken(userID int, username string) (string, error)
	ValidateToken(tokenString string) (jwt.MapClaims, error)
}

// JWTServiceImpl implements JWTService using golang-jwt
type JWTServiceImpl struct {
	secret []byte
}

// NewJWTService creates a new JWT service
func NewJWTService(secret string) JWTService {
	return &JWTServiceImpl{
		secret: []byte(secret),
	}
}

// GenerateToken generates a JWT token for a user
func (j *JWTServiceImpl) GenerateToken(userID int, username string) (string, error) {
	claims := jwt.MapClaims{
		"user_id":  userID,
		"username": username,
		"exp":      time.Now().Add(time.Hour * 24 * 7).Unix(), // 7 days
		"iat":      time.Now().Unix(),
	}

	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	return token.SignedString(j.secret)
}

// ValidateToken validates a JWT token and returns the claims
func (j *JWTServiceImpl) ValidateToken(tokenString string) (jwt.MapClaims, error) {
	token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
		if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
			return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
		}
		return j.secret, nil
	})

	if err != nil {
		return nil, err
	}

	if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
		return claims, nil
	}

	return nil, fmt.Errorf("invalid token")
}
