import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { ColorEntity } from 'src/modules/color/entities/color.entity';
import { BooleanTransformer } from 'src/utils/transformers/BooleanTransformer';
import { PictureProductMappingEntity } from './picture-product-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,
    transformer: new BooleanTransformer(),
  })
  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(
    () => PictureProductMappingEntity,
    (pictureProductMappings) => pictureProductMappings.picture,
  )
  pictureProductMappings: PictureProductMappingEntity[];

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