package com.example.todolist.entity;

import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Data;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.OneToMany;
import lombok.ToString;
import java.util.ArrayList;
import javax.persistence.OrderBy;

@Entity
@Table(name = "todo")
@Data
@ToString(exclude = "taskList")
public class Todo {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Integer id;

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

	@Column(name = "importance")
	private Integer importance;

	@Column(name = "urgency")
	private Integer urgency;

	@Column(name = "deadline")
	private Date deadline;

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

	@Column(name = "owner_id")
	private Integer ownerId;

	@Column(name = "completed_tasks")
	private Integer completedTasks;
	
	@ManyToOne
	@JoinColumns ({
		@JoinColumn(name="category_code", referencedColumnName = "code"),
		@JoinColumn(name="category_locale", referencedColumnName = "locale"),
	})
	private Category category;


	@OneToMany(mappedBy = "todo", cascade = CascadeType.ALL)
	@OrderBy("id asc")
	private List<Task> taskList = new ArrayList<>();

	@ManyToOne
	@JoinColumn(name = "groups_id")
	private Groups groups;
	
	// Todoへの参照設定
	public void addTask(Task task) {
		task.setTodo(this);
		taskList.add(task);
	}
}
