import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { UserRoleEntity } from './UserRole.entity';

@Index('name', ['name'], { unique: true })
@Entity('roles', { schema: DB_SCHEMA })
export class RolesEntity {
  @PrimaryGeneratedColumn({ type: 'tinyint', name: 'id' })
  id: number;

  @Column('varchar', { name: 'name', unique: true, length: 100 })
  name: string;

  @Column('json', { name: 'permissions', nullable: true })
  permissions: Record<string, any> | null;

  @Column('tinyint', {
    name: 'active',
    nullable: true,
    width: 1,
    default: () => "'1'",
  })
  active: boolean | null;

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

  @Column('timestamp', { name: 'created', default: () => 'CURRENT_TIMESTAMP' })
  created: Date;

  @Column('timestamp', { name: 'modified', default: () => 'CURRENT_TIMESTAMP' })
  modified: Date;

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