package ru.ulstu.user.model;

import ru.ulstu.core.model.BaseEntity;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Entity
@Table(name = "user_sessions")
public class UserSession extends BaseEntity {
    @NotNull
    @Column(name = "session_id", nullable = false, unique = true)
    private String sessionId;

    @NotNull
    @Column(name = "ip_address", nullable = false)
    private String ipAddress;

    @NotNull
    @Column(nullable = false)
    private String host;

    @NotNull
    @Column(name = "login_time", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date loginTime;

    @Column(name = "logout_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date logoutTime;

    @ManyToOne(optional = false)
    @JoinColumn(name = "user_id")
    private User user;

    public UserSession() {
    }

    public UserSession(String sessionId, String ipAddress, String host, User user) {
        this.sessionId = sessionId;
        this.ipAddress = ipAddress;
        this.host = host;
        this.loginTime = new Date();
        this.user = user;
    }

    public String getSessionId() {
        return sessionId;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public String getHost() {
        return host;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public Date getLogoutTime() {
        return logoutTime;
    }

    public User getUser() {
        return user;
    }

    public void close() {
        this.logoutTime = new Date();
    }
}
