import { AttributeOptionEntity } from 'src/modules/attribute/entities/attribute-option.entity';
import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
import { ProductEntity } from './product.entity';

@Index('specificationAttributeOptionId', ['specificationAttributeOptionId'], {})
@Entity('product_specificationattributeoption_mappings')
export class ProductAttributeMappingEntity extends BaseEntity {
  @Column('tinyint', { name: 'allowFiltering', nullable: true, width: 1 })
  allowFiltering: boolean | null;

  @Column('tinyint', { name: 'showOnProductPage', nullable: true, width: 1 })
  showOnProductPage: boolean | null;

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

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

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

  @Column('char', { primary: true, name: 'productId', length: 36 })
  productId: string;

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

  /**
   * Relations
   */
  @ManyToOne(() => ProductEntity, (products) => products.attributeMappings, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'productId', referencedColumnName: 'id' }])
  product: ProductEntity;

  @ManyToOne(
    () => AttributeOptionEntity,
    (attributeOptions) => attributeOptions.productAttributeOptionMappings,
    { onDelete: 'CASCADE', onUpdate: 'CASCADE' },
  )
  @JoinColumn([
    { name: 'specificationAttributeOptionId', referencedColumnName: 'id' },
  ])
  attributeOption: AttributeOptionEntity;
}
