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

@Index('menu_id', ['menu_id'], {})
@Entity('menu_actions', { schema: DB_SCHEMA })
export class MenuActionsEntity {
  @PrimaryGeneratedColumn({ type: 'mediumint', name: 'id', unsigned: true })
  id: number;

  @Column('smallint', { name: 'menu_id', unsigned: true })
  menu_id: number;

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

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

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

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

  @Column('tinyint', { name: 'platform', width: 1, default: () => "'1'" })
  platform: 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(() => MenusEntity, (menu) => menu.actions, {
    nullable: true,
    onDelete: 'SET NULL',
  })
  @JoinColumn({ name: 'menu_id' })
  menu: 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;
}
