package dev.ksan.etfoglasiserver.model;

import dev.ksan.etfoglasiserver.dto.UserCreationDTO;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Entity
@Table(name = "users")
public class User {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private UUID id;

  @Column(nullable = false, length = 255)
  private String email;

  @Column(nullable = false, length = 255)
  private String password;

  @Column(
          nullable = false,
          updatable = false,
          insertable = false,
          columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
  )
  private LocalDateTime regTime;

  @Enumerated(EnumType.STRING)
  @Column(name = "notification_type", nullable = false)
  private NotificationMethod notificationType =
          NotificationMethod.PUSH_NOTIFICATION;

  @ManyToMany(fetch = FetchType.LAZY)
  @JoinTable(
          name = "user_subject",
          joinColumns = @JoinColumn(name = "user_id"),
          inverseJoinColumns = @JoinColumn(name = "subject_id")
  )
  private Set<Subject> subjectSet;

  @OneToMany(
          mappedBy = "user",
          cascade = CascadeType.ALL,
          orphanRemoval = true,
          fetch = FetchType.LAZY
  )
  private List<DeviceToken> deviceTokens = new ArrayList<>();

  public void addDeviceToken(DeviceToken token) {
    deviceTokens.add(token);
    token.setUser(this);
  }

  public void removeDeviceToken(DeviceToken token) {
    deviceTokens.remove(token);
    token.setUser(null);
  }
  public User() {}

  public User(UserCreationDTO user) {
    this.email = user.getEmail();
    this.password = user.getPassword();
  }

  public UUID getId() {
    return id;
  }

  public void setId(UUID id) {
    this.id = id;
  }

  public String getEmail() {
    return email;
  }

  public void setEmail(String email) {
    this.email = email;
  }

  public String getPassword() {
    return password;
  }


  public void setPassword(String password) {
    this.password = password;
  }

  public LocalDateTime getRegTime() {
    return regTime;
  }

    public Set<Subject> getSubjectSet() {
        return subjectSet;
    }

    public void setSubjectSet(Set<Subject> subjectSet) {
        this.subjectSet = subjectSet;
    }



    public void setNotificationMethod(NotificationMethod notificationMethod) {
        this.notificationType = notificationMethod;
    }

  public NotificationMethod getNotificationMethod() {
    return notificationType;
  }

}
