import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
} from 'typeorm';

import { ProductEntity } from './product.entity';
import { ColorEntity } from 'src/modules/color/entities/color.entity';
import { PictureEntity } from './picture.entity';

@Index('colorId', ['colorId'], {})
@Index('pictureId', ['pictureId'], {})
@Entity('picture_product_mappings')
export class PictureProductMappingEntity 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.pictureProductMappings,
    {
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE',
    },
  )
  @JoinColumn([{ name: 'pictureId', referencedColumnName: 'id' }])
  picture: PictureEntity;
}
