import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { UserEntity } from './User.entity';
import { RolesEntity } from './Roles.entity';

@Entity('user_role', { schema: DB_SCHEMA })
export class UserRoleEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'user_role_id' })
  user_role_id: number;

  @Column('smallint', { name: 'user_id' })
  user_id: number;

  @Column('tinyint', { name: 'role_id' })
  role_id: number;

  @Column('smallint', { name: 'by_user', nullable: true })
  by_user: number | null;

  @ManyToOne(() => UserEntity, (user) => user.user_roles)
  @JoinColumn({ name: 'user_id' })
  user: UserEntity;

  @ManyToOne(() => RolesEntity, (role) => role.user_roles)
  @JoinColumn({ name: 'role_id' })
  role: RolesEntity;

  @OneToMany(() => UserRoleEntity, (ur) => ur.role)
  user_roles: UserRoleEntity[];
}
