package dev.ksan.etfoglasiserver.model;

import dev.ksan.etfoglasiserver.dto.EntryDTO;
import dev.ksan.etfoglasiserver.util.HashGenerator;
import jakarta.persistence.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Entity
@Table(name = "entries")
public class Entry {
  @Id
  @GeneratedValue
  @Column(columnDefinition = "uuid", updatable = false, nullable = false)
  private UUID id;


  @ManyToOne(fetch = FetchType.EAGER)
  @JoinColumn(nullable = false)
  private Subject subject;

  @Column private String title;
  @Column(name = "time_published")
  private LocalDateTime timePublished;

  @Column(name = "group_name")
  private String groupName;

  private String info_entry;
  private String paragraph;
  private String filepath;

  public Entry() {}

  public Entry(
      String title,
      LocalDateTime time_published,
      String info_entry,
      String paragraph,
      String filepath) {
    this.title = title;
    this.timePublished= time_published;
    this.info_entry = info_entry;
    this.paragraph = paragraph;
    this.filepath = filepath;
  }

  public Entry(
      String title,
      LocalDateTime time_published,
      String info_entry,
      List<String> paragraph,
      String filepath) {
    this.title = title;
    this.timePublished= time_published;
    this.info_entry = info_entry;
    this.paragraph = String.join("\n", paragraph);
    this.filepath = filepath;
  }
  public Entry( String title,String group, LocalDateTime time_published, String info_entry, List<String> paragraphs, String filepath, Subject subjectId) {
    this.title = title;
    this.groupName = group;
    this.timePublished= time_published;
    this.info_entry = info_entry;
    this.paragraph = String.join("\n",paragraphs);
    this.filepath = filepath;
    this.subject= subjectId;

  }
  public Entry(EntryDTO entry) {
    this.title = entry.getTitle();
    this.paragraph = entry.getParagraph();
    this.info_entry = entry.getInfo_entry();
    this.filepath = entry.getFilepath();
    this.timePublished= entry.getTime_published();
  }

  public String getParagraph() {
    return paragraph;
  }

  public Subject getSubject() {
    return subject;
  }

  public void setSubject(Subject subject) {
    this.subject = subject;
  }

  public void setParagraph(List<String> paragraphs) {
    this.paragraph = String.join("\n", paragraphs);
  }

  public void setParagraph(String paragraph) {
    this.paragraph = paragraph;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getTitle() {
    return title;
  }

  public LocalDateTime getTimePublished() {
    return timePublished;
  }

  public String getInfo() {
    return info_entry;
  }
  public UUID getId() {
    return id;
  }
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null) return false;
    Entry subject = (Entry) o;
    if (title.equals(subject.getTitle())
        && timePublished.equals(subject.getTimePublished())
        && info_entry.equals(subject.getInfo())) {
      return true;
    }
    return false;
  }

  @Override
  public String toString() {
    return "Entry{"
        + "id="
        + id
        + ", subject="
        + (subject != null ? subject.getId() : "null")
        + ", title='"
        + title
        + '\''
        + ", time_published="
        +  timePublished
        + ", info_entry='"
        + info_entry
        + '\''
        + ", paragraph='"
        + paragraph
        + '\''
        + ", filepath='"
        + filepath
        + '\''
        + '}';
  }

  public String getFilepath() {
    return filepath;
  }

  public void setFilepath(String filepath) {
    this.filepath = filepath;
  }

  public void setInfo_entry(String infoEntry) {
    this.info_entry = infoEntry;
  }

  public void setTime_published(LocalDateTime timePublished) {
    this.timePublished= timePublished;
  }

    public String getGroup() {
        return groupName;
    }

    public void setGroup(String group) {
        this.groupName = group;
    }
}
