package de.oaa.xxx.registration;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.Setter;

import java.security.SecureRandom;
import java.time.LocalDate;
import java.util.UUID;

@Getter
@Setter
@Entity
@Table(name = "registration")
public class RegistrationEntity {

    @Id
    @Column
    private UUID registrationId;
    @Column
    private String name;
    @Column(unique = true)
    private String email;
    @Column
    private String password;
    @Column
    private Boolean activated;
    @Column(length = 6)
    private String activationCode;
    @Column
    private LocalDate geburtsdatum;

    @Override
    public String toString() {
        return "RegistrationEntity [registrationId=" + registrationId + ", name=" + name + ", email=" + email + "]";
    }

    public Registration toRegistration() {
        Registration registration = new Registration();
        registration.setId(registrationId);
        registration.setEmail(email);
        registration.setName(name);
        registration.setPassword(password);
        registration.setGeburtsdatum(geburtsdatum);
        return registration;
    }

    public static RegistrationEntity create(Registration registration) {
        RegistrationEntity entity = new RegistrationEntity();
        entity.setRegistrationId(UUID.randomUUID());
        entity.setEmail(registration.getEmail());
        entity.setActivated(Boolean.FALSE);
        entity.setActivationCode(String.format("%06d", new SecureRandom().nextInt(1_000_000)));
        entity.setName(registration.getName());
        entity.setPassword(registration.getPassword());
        entity.setGeburtsdatum(registration.getGeburtsdatum());
        return entity;
    }
}
