package dev.dinauer.maven;

import at.favre.lib.crypto.bcrypt.BCrypt;
import dev.dinauer.maven.maven.core.MavenService;
import dev.dinauer.maven.maven.token.TokenEntity;
import dev.dinauer.maven.maven.token.TokenRepo;
import dev.dinauer.maven.maven.token.TokenService;
import dev.dinauer.maven.maven.token.dto.TokenCreation;
import dev.dinauer.maven.maven.token.dto.TokenSecret;
import io.quarkus.arc.profile.IfBuildProfile;
import io.quarkus.narayana.jta.QuarkusTransaction;
import io.quarkus.oidc.client.Tokens;
import io.quarkus.runtime.Startup;
import io.quarkus.security.identity.AuthenticationRequestContext;
import io.quarkus.security.identity.SecurityIdentity;
import io.quarkus.security.identity.SecurityIdentityAugmentor;
import io.quarkus.security.runtime.QuarkusPrincipal;
import io.quarkus.security.runtime.QuarkusSecurityIdentity;
import io.smallrye.mutiny.Uni;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.core.SecurityContext;
import org.jspecify.annotations.NonNull;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.Objects;
import java.util.UUID;

@ApplicationScoped
@IfBuildProfile("dev")
public class Dev
{
    private static final BCrypt.Hasher HASHER = BCrypt.withDefaults();

    @Inject
    TokenRepo tokenRepo;
    @Inject
    MavenService mavenService;

    @Startup
    public void create() throws IOException
    {
        QuarkusTransaction.begin();
        TokenEntity entity = new TokenEntity()
            .setName("test")
            .setToken(HASHER.hashToString(11, "c693b486-0f3a-4fe4-8716-de5a5a7fb566".toCharArray()))
            .setUserId("test")
            .setExpiresAt(LocalDate.now().plusDays(2))
            .setCreatedAt(ZonedDateTime.now());
        tokenRepo.persist(entity);
        QuarkusTransaction.commit();

        uploadSnapshot_01();
        uploadSnapshot_02();
    }

    private void uploadSnapshot_01() throws IOException
    {
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.123456-1.pom", readFile("/jar/postgresql-42.7.9.pom"));
    }

    private void uploadSnapshot_02() throws IOException
    {
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.133456-1.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.133456-1-javadoc.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.133456-1-tests.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.9-SNAPSHOT/postgresql-42.7.9-20250419.133456-1.pom", readFile("/jar/postgresql-42.7.9.pom"));
    }

    private byte[] readFile(String path) throws IOException
    {
        return Objects.requireNonNull(getClass().getResourceAsStream(path)).readAllBytes();
    }
}