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

import org.hibernate.annotations.Formula;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Set;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import io.delphiplatform.api.v3.model.video.VideoDspSlug;
import io.delphiplatform.api.v3.model.video.YoutubeContentType;
import io.delphiplatform.api.v3.rdb.entity.converter.VideoDspSlugAttributeConverter;
import io.delphiplatform.api.v3.rdb.entity.converter.YouTubeContentTypeAttributeConverter;
import lombok.Getter;
import lombok.Setter;

import static javax.persistence.FetchType.LAZY;

@Entity(name = "video")
@Getter
@Setter
public class VideoEntity {

    @Id
    private String videoId;
    private String dspVideoId;
    private String dspChannelId;
    @Convert(converter = VideoDspSlugAttributeConverter.class)
    private VideoDspSlug dsp;
    private String title;
    private Long durationSeconds;
    private LocalDate dateUploaded;
    private LocalDateTime publishedAt;
    @Convert(converter = YouTubeContentTypeAttributeConverter.class)
    private YoutubeContentType contentType;

    @Formula("(select video_isrc.views from video_isrc where video_isrc.video_id = video_id limit 1)")
    private Long views;
    @Column(name = "is_sony", nullable = false)
    private Boolean isSony;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "channel_id")
    private VideoChannelEntity channel;

    @ElementCollection(fetch = LAZY)
    @CollectionTable(name = "video_isrc", joinColumns = @JoinColumn(name = "video_id"))
    @Column(name = "isrc")
    private Set<String> isrcs;

}
