import { Collection, Entity, Index, ManyToMany, OneToMany, ManyToOne, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { Permission } from './permission.entity';
import { Restaurant } from '../../restaurants/entities/restaurant.entity';
import { RolePermission } from './rolePermission.entity';
import { AdminRole } from 'src/modules/admin/entities/adminRole.entity';

@Entity({ tableName: 'roles' })
@Index({ properties: ['restaurant'] })
export class Role extends BaseEntity {
  @Property()
  name!: string;

  @Property({ default: false })
  isSystem: boolean = false;

  @ManyToMany({ entity: () => Permission, pivotEntity: () => RolePermission, inversedBy: p => p.roles })
  permissions = new Collection<Permission>(this);

  @ManyToOne(() => Restaurant, { nullable: true })
  restaurant?: Restaurant;

  @OneToMany(() => AdminRole, adminRole => adminRole.role)
  admins = new Collection<AdminRole>(this);
}
