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

/**
 * Commerce Product Entity
 *
 * Synchronized with DDL: database/ddl/17-products.sql
 * Table: 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, pricing, attributes
 *   - 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
 */
@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;

  // Identification
  @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({ type: 'text', nullable: true })
  description: string;

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

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

  // Pricing
  @Column({ type: 'decimal', precision: 15, scale: 4, default: 0 })
  price: number;

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

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

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

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

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

  // Unit of measure
  @Column({ type: 'varchar', length: 20, default: 'PZA' })
  uom: string;

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

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

  // Inventory
  @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: 'lead_time_days', type: 'int', default: 0 })
  leadTimeDays: number;

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

  @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({ type: 'decimal', precision: 10, scale: 4, nullable: true })
  volume: number;

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

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

  // Attributes (flexible JSONB for custom attributes like color, size, material)
  @Column({ type: 'jsonb', default: '{}' })
  attributes: Record<string, unknown>;

  // Status
  @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;
}
