package dev.lions.unionflow.server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * Table de liaison entre Role et Permission
 * Permet à un rôle d'avoir plusieurs permissions
 *
 * @author UnionFlow Team
 * @version 3.0
 * @since 2025-01-29
 */
@Entity
@Table(
    name = "roles_permissions",
    indexes = {
      @Index(name = "idx_role_permission_role", columnList = "role_id"),
      @Index(name = "idx_role_permission_permission", columnList = "permission_id")
    },
    uniqueConstraints = {
      @UniqueConstraint(
          name = "uk_role_permission",
          columnNames = {"role_id", "permission_id"})
    })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
public class RolePermission extends BaseEntity {

  /** Rôle */
  @NotNull
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "role_id", nullable = false)
  private Role role;

  /** Permission */
  @NotNull
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "permission_id", nullable = false)
  private Permission permission;

  /** Commentaire sur l'association */
  @Column(name = "commentaire", length = 500)
  private String commentaire;
}

