package dev.dinauer.maven.maven.core.snapshot;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;

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

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

    private String date;

    private String time;

    @Column(name = "build_number")
    private int buildNumber;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "snapshot_version_id")
    @JsonBackReference
    private SnapshotVersion snapshotVersion;

    @OneToMany(mappedBy = "snapshotBundle", cascade = CascadeType.REMOVE)
    @JsonManagedReference
    private List<SnapshotJar> jars;

    @OneToOne(mappedBy = "snapshotBundle", cascade = CascadeType.REMOVE)
    @JsonManagedReference
    private SnapshotPom pom;

    @Column(name = "updated_at")
    private ZonedDateTime updatedAt;

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

    public String getId()
    {
        return id;
    }

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

    public String getDateTime()
    {
        return String.format("%s.%s", date, time);
    }

    public String getDate()
    {
        return date;
    }

    public SnapshotBundle setDate(String date)
    {
        this.date = date;
        return this;
    }

    public String getTime()
    {
        return time;
    }

    public SnapshotBundle setTime(String time)
    {
        this.time = time;
        return this;
    }

    public int getBuildNumber()
    {
        return buildNumber;
    }

    public SnapshotBundle setBuildNumber(int buildNumber)
    {
        this.buildNumber = buildNumber;
        return this;
    }

    public SnapshotVersion getSnapshotVersion()
    {
        return snapshotVersion;
    }

    public SnapshotBundle setSnapshotVersion(SnapshotVersion snapshotVersion)
    {
        this.snapshotVersion = snapshotVersion;
        return this;
    }

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

    public SnapshotBundle setJars(List<SnapshotJar> jars)
    {
        this.jars = jars;
        return this;
    }

    public SnapshotPom getPom()
    {
        return pom;
    }

    public SnapshotBundle setPom(SnapshotPom pom)
    {
        this.pom = pom;
        return this;
    }

    public ZonedDateTime getUpdatedAt()
    {
        return updatedAt;
    }

    public SnapshotBundle setUpdatedAt(ZonedDateTime updatedAt)
    {
        this.updatedAt = updatedAt;
        if (this.snapshotVersion != null)
        {
            this.snapshotVersion.setUpdatedAt(updatedAt);
        }
        return this;
    }

    public String getUploadedBy()
    {
        return uploadedBy;
    }

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