// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package auth

import (
	"net/http"

	"github.com/luxfi/log"
	apitypes "github.com/luxfi/api/types"
)

// Service that serves the Auth API functionality.
type Service struct {
	auth *auth
}

type Password struct {
	Password string `json:"password"` // The authorization password
}

type NewTokenArgs struct {
	Password
	// Endpoints that may be accessed with this token e.g. if endpoints is
	// ["/ext/bc/X", "/ext/admin"] then the token holder can hit the X-Chain API
	// and the admin API. If [Endpoints] contains an element "*" then the token
	// allows access to all API endpoints. [Endpoints] must have between 1 and
	// [maxEndpoints] elements
	Endpoints []string `json:"endpoints"`
}

type Token struct {
	Token string `json:"token"` // The new token. Expires in [TokenLifespan].
}

func (s *Service) NewToken(_ *http.Request, args *NewTokenArgs, reply *Token) error {
	s.auth.log.Debug("API called",
		log.String("service", "auth"),
		log.String("method", "newToken"),
	)

	var err error
	reply.Token, err = s.auth.NewToken(args.Password.Password, defaultTokenLifespan, args.Endpoints)
	return err
}

type RevokeTokenArgs struct {
	Password
	Token
}

func (s *Service) RevokeToken(_ *http.Request, args *RevokeTokenArgs, _ *apitypes.EmptyReply) error {
	s.auth.log.Debug("API called",
		log.String("service", "auth"),
		log.String("method", "revokeToken"),
	)

	return s.auth.RevokeToken(args.Token.Token, args.Password.Password)
}

type ChangePasswordArgs struct {
	OldPassword string `json:"oldPassword"` // Current authorization password
	NewPassword string `json:"newPassword"` // New authorization password
}

func (s *Service) ChangePassword(_ *http.Request, args *ChangePasswordArgs, _ *apitypes.EmptyReply) error {
	s.auth.log.Debug("API called",
		log.String("service", "auth"),
		log.String("method", "changePassword"),
	)

	return s.auth.ChangePassword(args.OldPassword, args.NewPassword)
}
