import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn, ManyToMany, JoinTable, Index } from 'typeorm';
import { GoodsCategoryMt } from './goods-category.entity.mt';
import { SupplierMt } from '@d8d/supplier-module-mt';
import { FileMt } from '@d8d/file-module-mt';
import { MerchantMt } from '@d8d/merchant-module-mt';

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

  @Index()
  @Column({ name: 'tenant_id', type: 'int', unsigned: true, comment: '租户ID' })
  tenantId!: 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(() => FileMt)
  @JoinTable({
    name: 'goods_slide_images',
    joinColumn: { name: 'goods_id', referencedColumnName: 'id' },
    inverseJoinColumn: { name: 'file_id', referencedColumnName: 'id' }
  })
  slideImages!: FileMt[];

  @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(() => GoodsCategoryMt, { nullable: true })
  @JoinColumn({ name: 'category_id1', referencedColumnName: 'id' })
  category1!: GoodsCategoryMt | null;

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

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

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

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

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