package de.cannamanage.domain.entity;

import de.cannamanage.domain.enums.StaffPermission;
import jakarta.persistence.*;

import java.time.Instant;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

/**
 * Staff account with fine-grained permissions.
 * Links a user (STAFF role) to their granted permissions stored as JSONB.
 * One StaffAccount per user; permissions are a subset of StaffPermission enum values.
 */
@Entity
@Table(name = "staff_accounts")
public class StaffAccount extends AbstractTenantEntity {

    @Column(name = "user_id", nullable = false, unique = true)
    private UUID userId;

    @Column(name = "display_name", nullable = false, length = 150)
    private String displayName;

    @ElementCollection(targetClass = StaffPermission.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "staff_account_permissions",
            joinColumns = @JoinColumn(name = "staff_account_id"))
    @Enumerated(EnumType.STRING)
    @Column(name = "permission", nullable = false, length = 50)
    private Set<StaffPermission> grantedPermissions = new HashSet<>();

    @Column(name = "is_prevention_officer", nullable = false)
    private boolean preventionOfficer = false;

    @Column(name = "active", nullable = false)
    private boolean active = true;

    @Column(name = "invited_at")
    private Instant invitedAt;

    @Column(name = "activated_at")
    private Instant activatedAt;

    // --- Getters & Setters ---

    public UUID getUserId() { return userId; }
    public void setUserId(UUID userId) { this.userId = userId; }

    public String getDisplayName() { return displayName; }
    public void setDisplayName(String displayName) { this.displayName = displayName; }

    public Set<StaffPermission> getGrantedPermissions() { return grantedPermissions; }
    public void setGrantedPermissions(Set<StaffPermission> grantedPermissions) { this.grantedPermissions = grantedPermissions; }

    public boolean isPreventionOfficer() { return preventionOfficer; }
    public void setPreventionOfficer(boolean preventionOfficer) { this.preventionOfficer = preventionOfficer; }

    public boolean isActive() { return active; }
    public void setActive(boolean active) { this.active = active; }

    public Instant getInvitedAt() { return invitedAt; }
    public void setInvitedAt(Instant invitedAt) { this.invitedAt = invitedAt; }

    public Instant getActivatedAt() { return activatedAt; }
    public void setActivatedAt(Instant activatedAt) { this.activatedAt = activatedAt; }

    public boolean hasPermission(StaffPermission permission) {
        return grantedPermissions.contains(permission);
    }
}
