import { ProductAttributeMappingEntity } from 'src/modules/product/entities/product-attribute-mapping.entity';
import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { AttributeEntity } from './attribute.entity';

@Index('specificationAttributeId', ['specificationAttributeId'], {})
@Entity('specificationattributeoptions')
export class AttributeOptionEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

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

  @Column('varchar', { name: 'name', nullable: true, length: 100 })
  name: string | null;

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

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

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

  /**
   * Relations
   */
  @OneToMany(
    () => ProductAttributeMappingEntity,
    (productSpecificationattributeoptionMappings) =>
      productSpecificationattributeoptionMappings.attributeOption,
  )
  productAttributeOptionMappings: ProductAttributeMappingEntity[];

  @ManyToOne(
    () => AttributeEntity,
    (attributes) => attributes.attributeOptions,
    { onDelete: 'CASCADE', onUpdate: 'CASCADE' },
  )
  @JoinColumn([
    { name: 'specificationAttributeId', referencedColumnName: 'id' },
  ])
  attribute: AttributeEntity;
}
