import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { MenuGroupsEntity } from './MenuGroups.entity';
import { MenusEntity } from './Menus.entity';
import { DesignationMastersEntity } from './DesignationMasters.entity';
import { CenterLocationEntity } from './CenterLocation.entity';
import { UserDivisionsEntity } from './UserDivisions.entity';
import { ReleaseNotesEntity } from './ReleaseNotes.entity';
import { ThemeEntity } from './Theme.entity';
import { FavoriteMenuEntity } from './FavoriteMenu.entity';
import { UserRoleEntity } from './UserRole.entity';
import { RecentVisitedMenusEntity } from './RecentVisitedMenus.entity';

@Index('employeeCode', ['employeeCode'], { unique: true })
@Index('personal_contact', ['personal_contact'], { unique: true })
@Index('personal_email', ['personal_email'], { unique: true })
@Entity('user', { schema: DB_SCHEMA })
export class UserEntity {
  @PrimaryGeneratedColumn({ type: 'smallint', name: 'id' })
  id: number;

  @Column('varchar', {
    name: 'employeeCode',
    nullable: true,
    unique: true,
    length: 7,
  })
  employeeCode: string | null;

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

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

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

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

  @Column('tinyint', { name: 'gender', nullable: true, width: 1 })
  gender: boolean | null;

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

  @Column('date', { name: 'dob', nullable: true })
  dob: string | null;

  @Column('date', { name: 'MA', nullable: true })
  MA: string | null;

  @Column('date', { name: 'DOJ', nullable: true })
  DOJ: string | null;

  @Column('datetime', { name: 'DOE', nullable: true })
  DOE: Date | null;

  @Column('date', { name: 'rejoining_date', nullable: true })
  rejoining_date: string | null;

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

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

  @Column('varchar', {
    name: 'personal_email',
    nullable: true,
    unique: true,
    length: 60,
  })
  personal_email: string | null;

  @Column('char', { name: 'mobile', nullable: true, length: 10 })
  mobile: string | null;

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

  @Column('char', {
    name: 'personal_contact',
    nullable: true,
    unique: true,
    length: 10,
  })
  personal_contact: string | null;

  @Column('varchar', {
    name: 'password',
    length: 50,
    default: () => "'fc5529b02a56939dca55d28baad15eba'",
  })
  password: string;

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

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

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

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

  @Column('tinyint', { name: 'mail_sent', nullable: true, width: 1 })
  mail_sent: boolean | null;

  @Column('tinyint', {
    name: 'password_change',
    width: 1,
    default: () => "'0'",
  })
  password_change: boolean;

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

  @Column('tinyint', { name: 'portal_access', width: 1, default: () => "'0'" })
  portal_access: boolean;

  @Column('tinyint', { name: 'role_modified', width: 1, default: () => "'0'" })
  role_modified: boolean;

  @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;

  @Column('tinyint', { name: 'waiver_power', nullable: true, width: 1 })
  waiver_power: boolean | null;

  @Column('tinyint', { name: 'refund_power', nullable: true, width: 1 })
  refund_power: boolean | null;

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

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

  @Column('json', {
    name: 'arranged_menu',
    nullable: true,
  })
  arranged_menu: object | null;

  @Column('json', {
    name: 'user_timezone',
    nullable: true,
  })
  user_timezone: object | null;

  @OneToMany(() => MenuGroupsEntity, (menu_groups) => menu_groups.user)
  menu_groups: MenuGroupsEntity[];

  @OneToMany(() => MenusEntity, (menus) => menus.user)
  menus_created: MenusEntity[];

  @OneToMany(() => MenusEntity, (menus) => menus.modified_by)
  menus_modified: MenusEntity[];

  @ManyToOne(
    () => DesignationMastersEntity,
    (designation) => designation.user,
    { nullable: true },
  )
  @JoinColumn({ name: 'emp_designation' })
  designation: UserEntity;

  @ManyToOne(() => CenterLocationEntity, (center) => center.users_home, {
    nullable: true,
  })
  @JoinColumn({ name: 'center_id_home' })
  center_home: CenterLocationEntity;

  @OneToMany(() => UserDivisionsEntity, (userDivision) => userDivision.user)
  user_divisions: UserDivisionsEntity[];

  @OneToMany(() => ReleaseNotesEntity, (release_notes) => release_notes.user)
  release_notes: ReleaseNotesEntity[];

  @OneToOne(() => ThemeEntity)
  @JoinColumn({ name: 'id', referencedColumnName: 'by_user' })
  theme: ThemeEntity;

  @OneToMany(() => FavoriteMenuEntity, (favoriteMenu) => favoriteMenu.user)
  favorite_menu: FavoriteMenuEntity[];

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

  @OneToOne(() => RecentVisitedMenusEntity, (recent) => recent.user)
  recent_visited_menus: RecentVisitedMenusEntity;
}
