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

@Index('contract_id', ['contract_id'], {})
@Entity('vcm_commission_contracts_history', {
  schema: DB_SCHEMA,
})
export class VcmCommissionContractsHistoryEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id', unsigned: true })
  id: number;

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

  @Column('json', { name: 'previous_content', nullable: true })
  previous_content: Record<any, any> | null;

  @Column('json', { name: 'current_content', nullable: true })
  current_content: Record<any, any> | null;

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

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

  @ManyToOne(
    () => VcmCommissionContractsEntity,
    (vcmCommissionContracts) =>
      vcmCommissionContracts.vcmCommissionContractsHistories,
    { onDelete: 'CASCADE', onUpdate: 'CASCADE' },
  )
  @JoinColumn([{ name: 'contract_id', referencedColumnName: 'id' }])
  contract: VcmCommissionContractsEntity;

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