package dev.dinauer.maven.maven.token;

import dev.dinauer.maven.maven.token.dto.Token;
import dev.dinauer.maven.maven.token.dto.TokenCreation;
import dev.dinauer.maven.maven.token.dto.TokenSecret;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.SecurityContext;

import java.util.List;

@Path("/tokens")
public class TokenResource
{
    @Inject
    TokenService tokenService;

    @Inject
    TokenRepo tokenRepo;

    @Inject
    SecurityContext securityContext;

    @GET
    public List<Token> get()
    {
        return tokenRepo.findByUserId(securityContext.getUserPrincipal().getName()).stream().map(item -> new Token(item.getName(), item.getExpiresAt(), item.getCreatedAt())).toList();
    }

    @POST
    @Transactional
    public TokenSecret create(TokenCreation tokenCreation)
    {
        return tokenService.create(tokenCreation);
    }
}
