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

@Index('active', ['active'], {})
@Entity('masteragent', { schema: DB_SCHEMA })
export class MasteragentEntity {
  @PrimaryGeneratedColumn({ type: 'smallint', name: 'id' })
  id: number;

  @Column('varchar', { name: 'uid', length: 15 })
  uid: string;

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

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

  @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: 'pin_code', nullable: true, length: 10 })
  pin_code: string | null;

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

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

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

  @Column('timestamp', { name: 'created', default: () => 'CURRENT_TIMESTAMP' })
  created: Date;

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

  @Column('timestamp', { name: 'modified', default: () => 'CURRENT_TIMESTAMP' })
  modified: Date;

  @OneToMany(
    () => MasteragentProductidsEntity,
    (maProducts) => maProducts.master_agent,
  )
  products: MasteragentProductidsEntity[];

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