package de.assecutor.votianlt.model;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

import java.time.LocalDateTime;

@Data
@Document(collection = "app_user")
public class AppUser {

    @Id
    @JsonIgnore
    private ObjectId id;

    @Field("bezeichnung")
    private String bezeichnung;

    @Field("vorname")
    private String vorname;

    @Field("nachname")
    private String nachname;

    @Field("telefon")
    private String telefon;

    @Field("app_code")
    private String appCode;

    @Field("email")
    @org.springframework.data.mongodb.core.index.Indexed(unique = true)
    private String email;

    @Field("password")
    private String password;

    // Reset-Token und Zeitstempel
    @Field("password_code")
    private String passwordCode;

    @Field("password_timestamp")
    private LocalDateTime passwordTimestamp;

    @Field("geraet")
    private String geraet;

    @Field("owner")
    private ObjectId owner;

    @Field("erstellt_am")
    private LocalDateTime erstelltAm;

    @Field("erstellt_von")
    private ObjectId erstelltVon;

    @Field("aktualisiert_am")
    private LocalDateTime aktualisiertAm;

    @Field("aktualisiert_von")
    private ObjectId aktualisiertVon;

    public AppUser() {
        this.erstelltAm = LocalDateTime.now();
        this.aktualisiertAm = LocalDateTime.now();
    }

    /**
     * Returns the ObjectId as string for JSON serialization. This ensures that the
     * app user id is returned as a string when users are retrieved via API.
     */
    @JsonGetter("id")
    public String getIdAsString() {
        return id != null ? id.toString() : null;
    }
}
