import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { VcmCommissionContractsEntity } from './VcmCommissionContracts.entity';
import { VcmCommissionContractSetsEntity } from './VcmCommissionContractSets.entity';

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

  @Column('mediumint', { name: 'commission_contract_id', unsigned: true })
  commission_contract_id: number;

  @Column('tinyint', { name: 'no_of_instances' })
  no_of_instances: number;

  @Column('tinyint', { name: 'students_per', comment: '1=Year, 2=Semester' })
  students_per: number;

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

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

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

  @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(
    () => VcmCommissionContractSetsEntity,
    (vcmCommissionContractSets) =>
      vcmCommissionContractSets.commission_contract,
  )
  vcm_commission_contract_sets: VcmCommissionContractSetsEntity[];

  @ManyToOne(
    () => VcmCommissionContractsEntity,
    (vcmCommissionContracts) =>
      vcmCommissionContracts.vcm_commission_contract_applicable_sets,
    { onDelete: 'NO ACTION', onUpdate: 'NO ACTION' },
  )
  @JoinColumn([
    {
      name: 'commission_contract_id',
      referencedColumnName: 'id',
    },
  ])
  commission_contract: VcmCommissionContractsEntity;
}
