import { DB_SCHEMA } from 'src/utils';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { VcmCommissionContractInstancesEntity } from './VcmCommissionContractInstances.entity';
import { VcmCommissionContractApplicableOnEntity } from './VcmCommissionContractApplicableOn.entity';

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

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

  @Column('varchar', { name: 'set_name', length: 250 })
  set_name: string;

  @Column('json', {
    name: 'attachment_file',
    nullable: true,
    comment: 'Specific Contract',
  })
  attachment_file: object | null;

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

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

  @Column('tinyint', { name: 'has_bonus', comment: '1=Yes, 2=No' })
  has_bonus: number;

  @Column('tinyint', {
    name: 'bonus_type',
    nullable: true,
    comment: '1=Fixed, 2=Percent',
  })
  bonus_type: number | null;

  @Column('decimal', {
    name: 'bonus_amount',
    nullable: true,
    precision: 11,
    scale: 2,
  })
  bonus_amount: string | null;

  @Column('decimal', {
    name: 'bonus_percentage',
    nullable: true,
    precision: 5,
    scale: 2,
  })
  bonus_percentage: string | 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(
    () => VcmCommissionContractInstancesEntity,
    (vcmCommissionContractInstances) =>
      vcmCommissionContractInstances.commission_contract_set,
  )
  vcm_commission_contract_instances: VcmCommissionContractInstancesEntity[];

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