import { Exclude } from 'class-transformer';
import {
  Column,
  Entity,
  ManyToMany,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import Permission from './permission.entity';
import { Timestamp } from './timestamp';
import { GenerateKey } from './generate-key.entity';

@Entity('admins')
export default class Admin extends Timestamp {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ type: 'varchar', unique: true })
  email: string;

  @Column({ type: 'varchar', unique: true })
  username: string;

  @Column({ type: 'varchar', default: null, nullable: true })
  fullname: string;

  @Column({ type: 'varchar' })
  @Exclude()
  password: string;

  @Column({ type: 'boolean', default: false })
  is_system_account: boolean;

  @ManyToMany(() => Permission, (permission) => permission.admins, {
    cascade: true,
    onDelete: 'CASCADE',
  })
  permissions: Permission[];

  @OneToMany(() => GenerateKey, (key) => key.admin, {
    cascade: true,
  })
  generateKeys: GenerateKey[];
}
