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

@Entity('menu_groups', { schema: DB_SCHEMA })
export class MenuGroupsEntity {
  @PrimaryGeneratedColumn({ type: 'smallint', name: 'id' })
  id: number;

  @Column('varchar', { name: 'name', length: 250 })
  name: string;

  @Column('smallint', { name: 'order_no', default: () => "'0'" })
  order_no: number;

  @Column('tinyint', { name: 'active', width: 1, default: () => "'1'" })
  active: number;

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

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

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

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

  @OneToMany(() => MenusEntity, (menu) => menu.group)
  menus: MenusEntity[];

  @ManyToOne(() => UserEntity, (user) => user.menu_groups, { nullable: true })
  @JoinColumn({ name: 'by_user' })
  user: UserEntity;

  @ManyToOne(() => UserEntity, { nullable: true })
  @JoinColumn({ name: 'modified_by_user' })
  modified_by: UserEntity;
}
