package de.effigenix.infrastructure.usermanagement.persistence.entity;

import de.effigenix.domain.usermanagement.Permission;
import de.effigenix.domain.usermanagement.RoleName;
import jakarta.persistence.*;

import java.util.HashSet;
import java.util.Set;

/**
 * JPA Entity for Role.
 * Infrastructure layer - NOT part of domain model!
 */
@Entity
@Table(name = "roles")
public class RoleEntity {

    @Id
    @Column(name = "id", nullable = false, length = 36)
    private String id;

    @Enumerated(EnumType.STRING)
    @Column(name = "name", nullable = false, unique = true, length = 50)
    private RoleName name;

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "role_permissions", joinColumns = @JoinColumn(name = "role_id"))
    @Enumerated(EnumType.STRING)
    @Column(name = "permission", nullable = false, length = 100)
    private Set<Permission> permissions = new HashSet<>();

    @Column(name = "description", length = 500)
    private String description;

    // JPA requires no-arg constructor
    protected RoleEntity() {
    }

    public RoleEntity(
            String id,
            RoleName name,
            Set<Permission> permissions,
            String description
    ) {
        this.id = id;
        this.name = name;
        this.permissions = permissions != null ? permissions : new HashSet<>();
        this.description = description;
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public RoleName getName() {
        return name;
    }

    public void setName(RoleName name) {
        this.name = name;
    }

    public Set<Permission> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<Permission> permissions) {
        this.permissions = permissions;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}
