import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { ColorEntity } from '../../color/entities/color.entity';
import { ProductPictureMappingEntity } from '../../product/entities/product-picture-mapping.entity';

@Index('colorId', ['colorId'], {})
@Entity('pictures')
export class PictureEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

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

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

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

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

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

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

  /**
   * Relations
   */
  @OneToMany(
    () => ProductPictureMappingEntity,
    (productMappings) => productMappings.picture,
  )
  productMappings: ProductPictureMappingEntity[];

  @ManyToOne(() => ColorEntity, (colors) => colors.pictures, {
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'colorId', referencedColumnName: 'id' }])
  color: ColorEntity;
}
