package com.jakubu9333.grammarManager.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.SequenceGenerator;
import java.time.LocalDateTime;

/**
 * @author Jakub Uhlarik
 */
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FileMetadata {

    @Id
    @JsonView(View.All.class)
    @SequenceGenerator(name = "file_sequence", allocationSize = 1, sequenceName = "file_sequence")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "file_sequence")
    private Long id;


    @OneToOne(fetch=FetchType.LAZY)
    @JsonIgnore
    private FileContent fileContent;

    @JsonView(View.All.class)
    private boolean xml;

    @JsonView(View.All.class)
    private boolean generated;

    @JsonView(View.All.class)
    private long size;

    @ManyToOne
    private Grammar grammar;

    private LocalDateTime updateTime;

    public FileMetadata(boolean xml, boolean generated, Grammar grammar) {
        this.xml = xml;
        this.generated = generated;
        this.grammar = grammar;
    }

    @PreUpdate
    public void prePersist() {
        this.updateTime = LocalDateTime.now();
    }

    @PreRemove
    public void preRemove() {

    }
}
