package online.wesal.wesal.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import jakarta.validation.constraints.Pattern;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Arrays;

@Entity
@Table(name = "users")
public class User {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(unique = true, nullable = false)
    @NotBlank
    @Pattern(regexp = "^\\+[1-9]\\d{1,14}$", message = "Phone number must be in international format starting with + and country code")
    private String phoneNumber;
    
    @Column(unique = true)
    private String username;
    
    @Column(nullable = false)
    @NotBlank
    @Size(min = 6)
    private String password;
    
    @Column(nullable = false)
    @NotBlank
    private String displayName;
    
    private String avatar;
    
    private String fcmToken;
    
    @Column(nullable = false)
    private boolean activated = false;
    
    @Column(nullable = false)
    private String role = "USER";
    
    @Column(columnDefinition = "TEXT")
    private String subscriptions = "[\"postcomments\",\"newinvites\",\"invitesfollowup\",\"appnews\"]";
    
    public User() {}
    
    public User(String phoneNumber, String password, String displayName) {
        this.phoneNumber = phoneNumber;
        this.password = password;
        this.displayName = displayName;
    }
    
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getPhoneNumber() {
        return phoneNumber;
    }
    
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    
    public String getUsername() {
        return username;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getDisplayName() {
        return displayName;
    }
    
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
    
    public String getAvatar() {
        return avatar;
    }
    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    public String getFcmToken() {
        return fcmToken;
    }
    
    public void setFcmToken(String fcmToken) {
        this.fcmToken = fcmToken;
    }
    
    public boolean isActivated() {
        return activated;
    }
    
    public void setActivated(boolean activated) {
        this.activated = activated;
    }
    
    public String getRole() {
        return role;
    }
    
    public void setRole(String role) {
        this.role = role;
    }
    
    public List<String> getSubscriptions() {
        if (subscriptions == null || subscriptions.isEmpty()) {
            return Arrays.asList("postcomments", "newinvites", "invitesfollowup", "appnews");
        }
        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readValue(subscriptions, new TypeReference<List<String>>() {});
        } catch (Exception e) {
            return Arrays.asList("postcomments", "newinvites", "invitesfollowup", "appnews");
        }
    }
    
    public void setSubscriptions(List<String> subscriptions) {
        try {
            ObjectMapper mapper = new ObjectMapper();
            this.subscriptions = mapper.writeValueAsString(subscriptions);
        } catch (Exception e) {
            this.subscriptions = "[\"postcomments\",\"newinvites\",\"invitesfollowup\",\"appnews\"]";
        }
    }
}