import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
} from 'typeorm';
import { ColorEntity } from '../../color/entities/color.entity';
import { PictureEntity } from '../../picture/entities/picture.entity';
import { ProductEntity } from './product.entity';

@Index('colorId', ['colorId'], {})
@Index('pictureId', ['pictureId'], {})
@Entity('picture_product_mappings')
export class ProductPictureMappingEntity extends BaseEntity {
  @Column('int', { name: 'colorId', nullable: true })
  colorId: number | 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: 'pictureId' })
  pictureId: number;

  /**
   * Relations
   */
  @ManyToOne(() => ColorEntity, (colors) => colors.pictureProductMappings, {
    onDelete: 'NO ACTION',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'colorId', referencedColumnName: 'id' }])
  color: ColorEntity;

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

  @ManyToOne(() => PictureEntity, (pictures) => pictures.productMappings, {
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'pictureId', referencedColumnName: 'id' }])
  picture: PictureEntity;
}
