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.security.SecureRandom;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

@ApplicationScoped
@IfBuildProfile("dev")
public class Dev implements SecurityIdentityAugmentor
{
    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();

        uploadRelease_01();
        uploadRelease_02();
        uploadSnapshot_01();
        uploadSnapshot_02();
        uploadSnapshot_03();
        uploadParent_01();
    }

    private void uploadRelease_01() throws IOException
    {
        mavenService.upload("/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.pom", readFile("/jar/postgresql-42.7.9.pom"));
        mavenService.upload("/org/postgresql/postgresql/42.7.8/postgresql-42.7.8.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.8/postgresql-42.7.8-tests.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.8/postgresql-42.7.8-javadocs.jar", readFile("/jar/postgresql-42.7.9.jar"));
    }

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

    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 void uploadSnapshot_03() throws IOException
    {
        mavenService.upload("/org/postgresql/postgresql/42.7.10-SNAPSHOT/postgresql-42.7.10-20250419.123456-1.jar", readFile("/jar/postgresql-42.7.9.jar"));
        mavenService.upload("/org/postgresql/postgresql/42.7.10-SNAPSHOT/postgresql-42.7.10-20250419.123456-1.pom", readFile("/jar/postgresql-42.7.9.pom"));
    }

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

    private void uploadParent_01() throws IOException
    {
        mavenService.upload("/dev/dinauer/maven-parent/1.0.0/maven-parent-1.0.0.pom", readFile("/jar/postgresql-42.7.9.pom"));
    }

    @Override
    public Uni<SecurityIdentity> augment(SecurityIdentity identity, AuthenticationRequestContext context)
    {
        return Uni.createFrom().item(QuarkusSecurityIdentity.builder().setPrincipal(new QuarkusPrincipal("67f2814b-f8af-42b2-be9e-8a43cde383ec")).build());
    }
}