package com.alterdekim.game.entity;

import com.alterdekim.game.component.game.ClubAccessType;
import com.alterdekim.game.component.game.RoleFlags;
import com.alterdekim.game.controller.result.api.ApiResult;
import com.alterdekim.game.controller.result.api.UserResult;
import com.alterdekim.game.repository.UserRepository;
import com.alterdekim.game.xml.NumericBooleanSerializer;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name="users")
public class User implements ApiResult {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Column(nullable = false, unique = true)
    private String username;

    @Column(nullable = false)
    @JsonIgnore
    private String password;

    @ManyToMany(fetch = FetchType.EAGER, cascade=CascadeType.MERGE)
    @JoinTable(
            name="users_roles",
            joinColumns={@JoinColumn(name="USER_ID", referencedColumnName="ID")},
            inverseJoinColumns={@JoinColumn(name="ROLE_ID", referencedColumnName="ID")})
    private List<Role> roles = new ArrayList<>();

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

    public User(Integer id, String username) {
        this.id = id;
        this.username = username;
    }

    @Override
    public UserResult toAPIResult() {
        return new UserResult(this.id, this.username, this.isBanned);
    }
}
