import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Product } from './product.entity';

/**
 * Product Variant Entity (schema: products.product_variants)
 *
 * Represents product variants generated from attribute combinations.
 * Example: "Blue T-Shirt - Size M" is a variant of product "T-Shirt".
 */
@Entity({ name: 'product_variants', schema: 'products' })
export class ProductVariant {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Index()
  @Column({ name: 'product_id', type: 'uuid' })
  productId: string;

  @ManyToOne(() => Product, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'product_id' })
  product: Product;

  @Index()
  @Column({ name: 'tenant_id', type: 'uuid' })
  tenantId: string;

  @Index()
  @Column({ type: 'varchar', length: 50 })
  sku: string;

  @Column({ type: 'varchar', length: 50, nullable: true })
  barcode: string;

  @Column({ type: 'varchar', length: 200 })
  name: string;

  @Column({ name: 'price_extra', type: 'decimal', precision: 15, scale: 4, default: 0 })
  priceExtra: number;

  @Column({ name: 'cost_extra', type: 'decimal', precision: 15, scale: 4, default: 0 })
  costExtra: number;

  @Column({ name: 'stock_qty', type: 'decimal', precision: 15, scale: 4, default: 0 })
  stockQty: number;

  @Column({ name: 'image_url', type: 'varchar', length: 500, nullable: true })
  imageUrl: string;

  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @CreateDateColumn({ name: 'created_at', type: 'timestamptz' })
  createdAt: Date;

  @Column({ name: 'created_by', type: 'uuid', nullable: true })
  createdBy: string;

  @UpdateDateColumn({ name: 'updated_at', type: 'timestamptz' })
  updatedAt: Date;

  @Column({ name: 'updated_by', type: 'uuid', nullable: true })
  updatedBy: string;
}
