package io.delphiplatform.api.v3.rdb.entity;

import org.hibernate.annotations.Formula;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import io.delphiplatform.api.v3.rdb.entity.apple.music.PlaylistPublicViewId;
import lombok.Getter;
import lombok.Setter;

@Entity(name = "playlist_combined_view")
@Getter
@Setter
public class PlaylistPublicViewEntity {

    //for this view entity - PK is 'playlistId+countryCode' but underlying DB tables that are unified into this view
    //may actually have PK in DB as just 'playlistId' OR as indeed both 'playlistId+countryCode' (Apple)
    @EmbeddedId
    private PlaylistPublicViewId id;

    @Column(name = "playlist_id", insertable = false, updatable = false)
    private String playlistId;
    private String name;
    private Integer numTracks;
    private String playlistType;
    private String uri;
    private String artworkUrl;
    @Column(nullable = false)
    private String dspPlaylistId;
    @Column(name = "description")
    private String description;
    @Column(name = "followers")
    private Integer followers;
    @Column(name = "is_ignored")
    private Boolean ignored;
    @Column(name = "is_removed")
    private Boolean removed;
    @Column(name = "is_personalised")
    private Boolean personalised;
    @Column(name = "is_public")
    private Boolean publicPlaylist;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dsp_id", nullable = false)
    private DspEntity dsp;

    @Column(name = "country_code", insertable = false, updatable = false)
    private String countryCode;

    @Column(name = "owner_username")
    private String ownerUserName;

    @Formula("(select min(rank.rank) from playlist_rank_combined_view rank where rank.playlist_id = playlist_id"
        + " and (dsp_id != 'apple' or rank.country_code = country_code) and rank.is_latest = 'true')")
    private Integer rank;
}
