package app.mealsmadeeasy.api.auth;

import app.mealsmadeeasy.api.user.User;
import jakarta.persistence.*;
import lombok.Data;

import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.UUID;

@Entity
@Table(name = "refresh_token")
@Data
public class RefreshToken {

    @Id
    @Column(nullable = false)
    private UUID token;

    @Column(nullable = false)
    private OffsetDateTime issued;

    @Column(nullable = false)
    private OffsetDateTime expiration;

    @ManyToOne(optional = false)
    @JoinColumn(name = "owner_id", nullable = false)
    private User owner;

    @Column(nullable = false)
    private Boolean deleted = false;

    @Column(nullable = false)
    private Boolean revoked = false;

    public long getLifetime() {
        return ChronoUnit.SECONDS.between(this.issued, this.expiration);
    }

}
