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

@Entity('user_divisions', { schema: DB_SCHEMA })
export class UserDivisionsEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

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

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

  @Column('tinyint', { 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;

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

  @ManyToOne(
    () => DivisionMastersEntity,
    (division) => division.user_divisions,
    { nullable: true },
  )
  @JoinColumn({ name: 'division_id' })
  division: DivisionMastersEntity;
}
