package studycave.studycaverestservice.model.test;

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 org.springframework.lang.Nullable;
import studycave.studycaverestservice.model.studyGroup.StudyGroup;
import studycave.studycaverestservice.model.test.question.Question;
import studycave.studycaverestservice.model.userActivity.UserActivity;

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

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Test {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;

    private String title;

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

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

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

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

    private String permission;

    private int grade;

    @JsonProperty("body")
    @OneToMany(fetch = FetchType.LAZY,mappedBy="test",cascade = CascadeType.ALL)
    @JsonManagedReference
    List<Question> questions = new ArrayList<>();

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

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

    @Nullable
    private Long availableFrom = null;

    @Nullable
    private Long availableTo = null;




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

    public void setGrade() {
        this.grade = 0;
    }

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