package studycave.studycaverestservice.model.flashcardset;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import studycave.studycaverestservice.model.flashcard.Flashcard;
import studycave.studycaverestservice.model.studyGroup.StudyGroup;
import studycave.studycaverestservice.model.userActivity.UserActivity;

import javax.persistence.*;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "flashcardset")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Set {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String category;

    @Column(name="id_owner")
    @JsonProperty("owner")
    private int idOwner;

    @Column(name="add_date")
    @JsonProperty("add_date")
    private Date addDate;

    @Column(name="edit_date")
    @JsonProperty("edit_date")
    private Date editDate;

    private int grade;

    @Column(name="permission")
    private String permission;

    @Column(name="status")
    private String status;

    @OneToMany(mappedBy="flashcardSet",cascade = CascadeType.ALL)
    @JsonManagedReference
    List<Flashcard> flashcards = new ArrayList<>();

    @ManyToOne
    @JoinColumn(name="group_id",referencedColumnName="id")
    private StudyGroup group;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "set", cascade = CascadeType.ALL)
    private List<UserActivity> activity = new ArrayList<>();

    public Set(String name, String category, int idOwner) {
        super();
        this.name = name;
        this.category = category;
        this.idOwner = idOwner;
    }

    public void setEditDate() {
        java.util.Date utilDate = new java.util.Date();
        Date sqlDate = new Date(utilDate.getTime());
        this.editDate = sqlDate;
    }

    public void setAddDate() {
        java.util.Date utilDate = new java.util.Date();
        Date sqlDate = new Date(utilDate.getTime());
        this.addDate = sqlDate;
    }
}
