import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn, ManyToMany, JoinTable } from 'typeorm';
import { GoodsCategory } from './goods-category.entity.js';
import { Supplier } from '@d8d/supplier-module';
import { File } from '@d8d/file-module';
import { Merchant } from '@d8d/merchant-module';

@Entity('goods')
export class Goods {
  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;

  @Column({ name: 'name', type: 'varchar', length: 255, comment: '商品名称' })
  name!: string;

  @Column({ name: 'price', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '售卖价' })
  price!: number;

  @Column({ name: 'cost_price', type: 'decimal', precision: 10, scale: 2, default: 0.00, comment: '成本价' })
  costPrice!: number;

  @Column({ name: 'sales_num', type: 'bigint', unsigned: true, default: 0, comment: '销售数量' })
  salesNum!: number;

  @Column({ name: 'click_num', type: 'bigint', unsigned: true, default: 0, comment: '点击次数' })
  clickNum!: number;

  @Column({ name: 'category_id1', type: 'int', unsigned: true, default: 0, comment: '一级类别id' })
  categoryId1!: number;

  @Column({ name: 'category_id2', type: 'int', unsigned: true, default: 0, comment: '二级类别id' })
  categoryId2!: number;

  @Column({ name: 'category_id3', type: 'int', unsigned: true, default: 0, comment: '三级类别id' })
  categoryId3!: number;

  @Column({ name: 'goods_type', type: 'smallint', unsigned: true, default: 1, comment: '订单类型 1实物产品 2虚拟产品' })
  goodsType!: number;

  @Column({ name: 'supplier_id', type: 'int', unsigned: true, nullable: true, comment: '所属供应商id' })
  supplierId!: number | null;

  @Column({ name: 'merchant_id', type: 'int', unsigned: true, nullable: true, comment: '所属商户id' })
  merchantId!: number | null;

  @Column({ name: 'image_file_id', type: 'int', unsigned: true, nullable: true, comment: '商品主图文件ID' })
  imageFileId!: number | null;

  @ManyToMany(() => File)
  @JoinTable({
    name: 'goods_slide_images',
    joinColumn: { name: 'goods_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'file_id', referencedColumnName: 'id' }
  })
  slideImages!: File[];

  @Column({ name: 'detail', type: 'text', nullable: true, comment: '商品详情' })
  detail!: string | null;

  @Column({ name: 'instructions', type: 'varchar', length: 255, nullable: true, comment: '简介' })
  instructions!: string | null;

  @Column({ name: 'sort', type: 'int', unsigned: true, default: 0, comment: '排序' })
  sort!: number;

  @Column({ name: 'state', type: 'smallint', unsigned: true, default: 1, comment: '状态 1可用 2不可用' })
  state!: number;

  @Column({ name: 'stock', type: 'bigint', unsigned: true, default: 0, comment: '库存' })
  stock!: number;

  @Column({ name: 'spu_id', type: 'int', unsigned: true, default: 0, comment: '主商品ID' })
  spuId!: number;

  @Column({ name: 'spu_name', type: 'varchar', length: 255, nullable: true, comment: '主商品名称' })
  spuName!: string | null;

  @Column({ name: 'lowest_buy', type: 'int', unsigned: true, default: 1, comment: '最小起购量' })
  lowestBuy!: number;

  @CreateDateColumn({ name: 'created_at', type: 'timestamp', comment: '创建时间' })
  createdAt!: Date;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamp', comment: '更新时间' })
  updatedAt!: Date;

  @Column({ name: 'created_by', type: 'int', unsigned: true, nullable: true, comment: '创建用户ID' })
  createdBy!: number | null;

  @Column({ name: 'updated_by', type: 'int', unsigned: true, nullable: true, comment: '更新用户ID' })
  updatedBy!: number | null;

  @ManyToOne(() => GoodsCategory, { nullable: true })
  @JoinColumn({ name: 'category_id1', referencedColumnName: 'id' })
  category1!: GoodsCategory | null;

  @ManyToOne(() => GoodsCategory, { nullable: true })
  @JoinColumn({ name: 'category_id2', referencedColumnName: 'id' })
  category2!: GoodsCategory | null;

  @ManyToOne(() => GoodsCategory, { nullable: true })
  @JoinColumn({ name: 'category_id3', referencedColumnName: 'id' })
  category3!: GoodsCategory | null;

  @ManyToOne(() => Supplier, { nullable: true })
  @JoinColumn({ name: 'supplier_id', referencedColumnName: 'id' })
  supplier!: Supplier | null;

  @ManyToOne(() => File, { nullable: true })
  @JoinColumn({ name: 'image_file_id', referencedColumnName: 'id' })
  imageFile!: File | null;

  @ManyToOne(() => Merchant, { nullable: true })
  @JoinColumn({ name: 'merchant_id', referencedColumnName: 'id' })
  merchant!: Merchant | null;
}