import {
  BaseEntity,
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { PictureProductMappingEntity } from './picture-product-mapping.entity';
import { PictureEntity } from './picture.entity';
import { ProductEntity } from './product.entity';

@Index('productId', ['productId'], {})
@Entity('colors')
export class ColorEntity extends BaseEntity {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id' })
  id: number;

  @Column('char', { name: 'productId', nullable: true, length: 36 })
  productId: string | null;

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

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

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

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

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

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

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

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

  /**
   * Relations
   */
  @ManyToOne(() => ProductEntity, (products) => products.colors, {
    onDelete: 'NO ACTION',
    onUpdate: 'CASCADE',
  })
  @JoinColumn([{ name: 'productId', referencedColumnName: 'id' }])
  product: ProductEntity;

  @OneToMany(
    () => PictureProductMappingEntity,
    (pictureProductMappings) => pictureProductMappings.color,
  )
  pictureProductMappings: PictureProductMappingEntity[];

  @OneToMany(() => PictureEntity, (pictures) => pictures.color)
  pictures: PictureEntity[];
}
