import { PermissionAlreadyAssignedException, User } from '@api/auth';
import { Column, Entity, JoinTable, ManyToMany, PrimaryColumn } from 'typeorm';
import { Permission } from './permission';

/**
 * Represents a role in the authentication system.
 */
@Entity()
export class Role {
  @PrimaryColumn('uuid')
  id: string;
  @Column()
  name: string;
  @Column()
  description: string;
  @ManyToMany(t => Permission, t => t.roles)
  @JoinTable({ name: 'role_permission' })
  permissions: Permission[];
  @ManyToMany(t => User, t => t.roles)
  users: User[];

  constructor(props: Partial<Role>) {
    Object.assign(this, props);
  }

  /**
   * Assigns a permission to this role.
   * @param permission the permission to assign
   */
  assignPermission(permission: Permission) {
    // Check existing permission
    const existingPerms = this.permissions
      .find(p => p.id === permission.id);

    if (existingPerms) 


    this.permissions.push(permission);
  }
}

{
      throw new PermissionAlreadyAssignedException('Permission is already assigned to this role!');
    }