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

/**
 * Commerce Product Entity (schema: products.products)
 *
 * NOTE: This is NOT a duplicate of inventory/entities/product.entity.ts
 *
 * Key differences:
 * - This entity: products.products - Commerce/retail focused
 *   - Has: SAT codes, tax rates, detailed dimensions, min/max stock, reorder points
 *   - Used by: Sales, purchases, invoicing, POS
 *
 * - Inventory Product: inventory.products - Warehouse/stock management focused (Odoo-style)
 *   - Has: valuationMethod, tracking (lot/serial), isStorable, StockQuant/Lot relations
 *   - Used by: Inventory module for stock tracking, valuation, picking operations
 *
 * These are intentionally separate by domain. This commerce product entity handles
 * pricing, tax compliance (SAT/CFDI), and business rules. For physical stock tracking,
 * use the inventory module's product entity.
 */
@Entity({ name: 'products', schema: 'products' })
export class Product {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Index()
  @Column({ name: 'category_id', type: 'uuid', nullable: true })
  categoryId: string;

  @ManyToOne(() => ProductCategory, { nullable: true, onDelete: 'SET NULL' })
  @JoinColumn({ name: 'category_id' })
  category: ProductCategory;

  /**
   * Optional link to inventory.products for unified stock management.
   * This allows the commerce product to be linked to its inventory counterpart
   * for stock tracking, valuation (FIFO/AVERAGE), and warehouse operations.
   *
   * The inventory product handles: stock levels, lot/serial tracking, valuation layers
   * This commerce product handles: pricing, taxes, SAT compliance, commercial data
   */
  @Index()
  @Column({ name: 'inventory_product_id', type: 'uuid', nullable: true })
  inventoryProductId: string | null;

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

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

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

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

  @Column({ type: 'text', nullable: true })
  description: string;

  // Tipo
  @Index()
  @Column({ name: 'product_type', type: 'varchar', length: 20, default: 'product' })
  productType: 'product' | 'service' | 'consumable' | 'kit';

  // Precios
  @Column({ name: 'sale_price', type: 'decimal', precision: 15, scale: 4, default: 0 })
  salePrice: number;

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

  @Column({ name: 'min_sale_price', type: 'decimal', precision: 15, scale: 4, nullable: true })
  minSalePrice: number;

  @Column({ type: 'varchar', length: 3, default: 'MXN' })
  currency: string;

  // Impuestos
  @Column({ name: 'tax_rate', type: 'decimal', precision: 5, scale: 2, default: 16 })
  taxRate: number;

  @Column({ name: 'tax_included', type: 'boolean', default: false })
  taxIncluded: boolean;

  // SAT (Mexico)
  @Column({ name: 'sat_product_code', type: 'varchar', length: 20, nullable: true })
  satProductCode: string;

  @Column({ name: 'sat_unit_code', type: 'varchar', length: 10, nullable: true })
  satUnitCode: string;

  // Unidad de medida
  @Column({ type: 'varchar', length: 20, default: 'PZA' })
  uom: string;

  @Column({ name: 'uom_purchase', type: 'varchar', length: 20, nullable: true })
  uomPurchase: string;

  @Column({ name: 'conversion_factor', type: 'decimal', precision: 10, scale: 4, default: 1 })
  conversionFactor: number;

  // Inventario
  @Column({ name: 'track_inventory', type: 'boolean', default: true })
  trackInventory: boolean;

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

  @Column({ name: 'max_stock', type: 'decimal', precision: 15, scale: 4, nullable: true })
  maxStock: number;

  @Column({ name: 'reorder_point', type: 'decimal', precision: 15, scale: 4, nullable: true })
  reorderPoint: number;

  @Column({ name: 'reorder_quantity', type: 'decimal', precision: 15, scale: 4, nullable: true })
  reorderQuantity: number;

  // Lotes y series
  @Column({ name: 'track_lots', type: 'boolean', default: false })
  trackLots: boolean;

  @Column({ name: 'track_serials', type: 'boolean', default: false })
  trackSerials: boolean;

  @Column({ name: 'track_expiry', type: 'boolean', default: false })
  trackExpiry: boolean;

  // Dimensiones
  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  weight: number;

  @Column({ name: 'weight_unit', type: 'varchar', length: 10, default: 'kg' })
  weightUnit: string;

  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  length: number;

  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  width: number;

  @Column({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  height: number;

  @Column({ name: 'dimension_unit', type: 'varchar', length: 10, default: 'cm' })
  dimensionUnit: string;

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

  @Column({ type: 'text', array: true, default: '{}' })
  images: string[];

  // Tags
  @Column({ type: 'text', array: true, default: '{}' })
  tags: string[];

  // Notas
  @Column({ type: 'text', nullable: true })
  notes: string;

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

  @Column({ name: 'is_sellable', type: 'boolean', default: true })
  isSellable: boolean;

  @Column({ name: 'is_purchasable', type: 'boolean', default: true })
  isPurchasable: boolean;

  // Metadata
  @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;

  @DeleteDateColumn({ name: 'deleted_at', type: 'timestamptz', nullable: true })
  deletedAt: Date;
}
