import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { UserEntity } from '../../domain/entities/user.entity';
import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  OneToOne,
} from 'typeorm';
import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model';
import { UserPrivilegeModel } from 'src/modules/user-related/user-privilege/data/models/user-privilege.model';
import { UserRole } from '../../constants';
import { ItemModel } from 'src/modules/item-related/item/data/models/item.model';
import { UserLoginModel } from './user-login.model';

@Entity(TABLE_NAME.USER)
export class UserModel
  extends BaseStatusModel<UserEntity>
  implements UserEntity
{
  @Column('varchar', { name: 'name', nullable: true })
  name: string;

  @Column('varchar', { name: 'username', nullable: true })
  username: string;

  @Column('varchar', { name: 'password', nullable: true })
  password: string;

  @Column('enum', { name: 'role', enum: UserRole, default: UserRole.STAFF })
  role: UserRole;

  @Column('varchar', { name: 'user_privilege_id', nullable: true })
  user_privilege_id: string;
  @ManyToOne(() => UserPrivilegeModel, (model) => model.users, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn({ name: 'user_privilege_id' })
  user_privilege: UserPrivilegeModel;

  // tenant data
  @Column('decimal', { name: 'share_margin', nullable: true })
  share_margin: number;

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

  // relasi ke tenant item
  @OneToMany(() => ItemModel, (model) => model.tenant, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  items: ItemModel[];

  // relasi ke user login for admin queue
  @OneToMany(() => UserLoginModel, (model) => model.user, {
    cascade: true,
  })
  user_login: UserLoginModel;
}
