package dev.dinauer.maven.jpa.maven;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import dev.dinauer.maven.user.User;
import jakarta.persistence.*;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Entity
@Table(name = "version")
public class Version
{
    @Id
    @GeneratedValue(strategy = GenerationType.UUID)
    private String id;

    @Column(name = "group_id_long")
    private String groupId;

    @Column(name = "artifact_id_long")
    private String artifactId;

    private String version;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "artifact_id")
    @JsonBackReference
    private ArtifactId artifact;

    @OneToMany(mappedBy = "version", cascade = CascadeType.ALL)
    @JsonManagedReference
    private List<Jar> jars = new ArrayList<>();

    @OneToOne(mappedBy = "version", cascade = CascadeType.ALL)
    @JsonManagedReference
    private Pom pom;

    @Column(name = "last_pulled")
    private ZonedDateTime lastPulled;

    @Column(name = "pull_count")
    private int pullCount = 0;

    @Column(name = "uploaded_by")
    private String uploadedBy;

    @Transient
    private User uploadUser;

    public String getId()
    {
        return id;
    }

    public Version setId(String id)
    {
        this.id = id;
        return this;
    }

    public String getGroupId()
    {
        return groupId;
    }

    public Version setGroupId(String groupId)
    {
        this.groupId = groupId;
        return this;
    }

    public String getArtifactId()
    {
        return artifactId;
    }

    public Version setArtifactId(String artifactId)
    {
        this.artifactId = artifactId;
        return this;
    }

    public String getVersion()
    {
        return version;
    }

    public Version setVersion(String version)
    {
        this.version = version;
        return this;
    }

    public ArtifactId getArtifact()
    {
        return artifact;
    }

    public Version setArtifact(ArtifactId artifactId)
    {
        this.artifact = artifactId;
        return this;
    }

    public List<Jar> getJars()
    {
        return jars;
    }

    public Version setJars(List<Jar> jars)
    {
        this.jars = jars;
        return this;
    }

    public Pom getPom()
    {
        return pom;
    }

    public Version setPom(Pom pom)
    {
        this.pom = pom;
        return this;
    }

    public ZonedDateTime getLastPulled()
    {
        return lastPulled;
    }

    public Version setLastPulled(ZonedDateTime lastPulled)
    {
        this.lastPulled = lastPulled;
        return this;
    }

    public String getUploadedBy()
    {
        return uploadedBy;
    }

    public Version setUploadedBy(String uploadedBy)
    {
        this.uploadedBy = uploadedBy;
        return this;
    }

    public User getUploadAccount()
    {
        return uploadUser;
    }

    public Version setUploadAccount(User uploadUser)
    {
        this.uploadUser = uploadUser;
        return this;
    }

    public int getPullCount()
    {
        return pullCount;
    }

    public Version setPullCount(int pullCount)
    {
        this.pullCount = pullCount;
        return this;
    }

    public void incrementPullCount()
    {
        pullCount = pullCount + 1;
    }

    public Optional<Jar> getJarByFilename(String filename)
    {
        for (Jar jar : jars)
        {
            if (jar.getFilename().equals(filename))
            {
                return Optional.of(jar);
            }
        }
        return Optional.empty();
    }
}
