import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { MenuGroupsEntity } from './MenuGroups.entity';
import { UserEntity } from './User.entity';
import { MenuActionsEntity } from './MenuActions.entity';

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

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

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

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

  @Column('varchar', { name: 'icon', nullable: true, length: 100 })
  icon: string | null;

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

  @Column('tinyint', { name: 'is_visible', width: 1, default: () => "'1'" })
  is_visible: 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;

  @ManyToOne(() => MenuGroupsEntity, (group) => group.menus, {
    nullable: true,
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'menu_group_id' })
  group: MenuGroupsEntity;

  @OneToMany(() => MenuActionsEntity, (action) => action.menu)
  actions: MenuActionsEntity[];

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

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