import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
import { DiscountEntity } from '../../discount/entities/discount.entity';
import { CategoryEntity } from './category.entity';

@Index('categoryId', ['categoryId'], {})
@Entity('category_discount_mappings')
export class CategoryDiscountMappingEntity extends BaseEntity {
  @Column('datetime', { name: 'createdAt' })
  createdAt: Date;

  @Column('datetime', { name: 'updatedAt' })
  updatedAt: Date;

  @Column('int', { primary: true, name: 'discountId' })
  discountId: number;

  @Column('int', { primary: true, name: 'categoryId' })
  categoryId: number;

  /**
   * Relations
   */
  @ManyToOne(
    () => DiscountEntity,
    (discounts) => discounts.categoryDiscountMappings,
    {
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
  )
  @JoinColumn([{ name: 'discountId', referencedColumnName: 'id' }])
  discount: DiscountEntity;

  @ManyToOne(
    () => CategoryEntity,
    (categories) => categories.categoryDiscountMappings,
    { onDelete: 'CASCADE', onUpdate: 'CASCADE' },
  )
  @JoinColumn([{ name: 'categoryId', referencedColumnName: 'id' }])
  category: CategoryEntity;
}
