import { Cascade, Collection, Entity, OneToMany, Property } from '@mikro-orm/core';
import { BaseEntity } from '../../../common/entities/base.entity';
import { AdminRole } from './adminRole.entity';
import { normalizePhone } from '../../utils/phone.util';

@Entity({ tableName: 'admins' })
export class Admin extends BaseEntity {
  @Property({ nullable: true })
  firstName?: string;

  @Property({ nullable: true })
  lastName?: string;

  private _phone!: string;

  @Property({ unique: true })
  get phone(): string {
    return this._phone;
  }

  set phone(value: string) {
    this._phone = normalizePhone(value);
  }

  @OneToMany(() => AdminRole, adminRole => adminRole.admin, {
    cascade: [Cascade.ALL],
    orphanRemoval: true,
  })
  roles = new Collection<AdminRole>(this);

}
