/**
 * Part Entity
 * Mecánicas Diesel - ERP Suite
 *
 * Represents parts/spare parts inventory.
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
  Check,
} from 'typeorm';
import { PartCategory } from './part-category.entity';
import { Supplier } from './supplier.entity';
import { WarehouseLocation } from './warehouse-location.entity';

@Entity({ name: 'parts', schema: 'parts_management' })
@Index('idx_parts_tenant', ['tenantId'])
@Index('idx_parts_sku', ['sku'])
@Index('idx_parts_barcode', ['barcode'])
@Index('idx_parts_category', ['categoryId'])
@Index('idx_parts_supplier', ['preferredSupplierId'])
@Check('chk_min_max_stock', '"max_stock" IS NULL OR "max_stock" >= "min_stock"')
export class Part {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({ type: 'varchar', length: 100, nullable: true })
  brand?: string;

  @Column({ type: 'varchar', length: 100, nullable: true })
  manufacturer?: string;

  @Column({ name: 'compatible_engines', type: 'text', array: true, nullable: true })
  compatibleEngines?: string[];

  // Pricing
  @Column({ type: 'decimal', precision: 12, scale: 2, nullable: true })
  cost?: number;

  @Column({ type: 'decimal', precision: 12, scale: 2 })
  price: number;

  // Inventory
  @Column({ name: 'current_stock', type: 'decimal', precision: 10, scale: 3, default: 0 })
  currentStock: number;

  @Column({ name: 'reserved_stock', type: 'decimal', precision: 10, scale: 3, default: 0 })
  reservedStock: number;

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

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

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

  @Column({ name: 'location_id', type: 'uuid', nullable: true })
  locationId?: string;

  @Column({ type: 'varchar', length: 20, default: 'pza' })
  unit: string;

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

  @Column({ name: 'preferred_supplier_id', type: 'uuid', nullable: true })
  preferredSupplierId?: string;

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

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

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

  // Computed
  get availableStock(): number {
    return this.currentStock - this.reservedStock;
  }

  get isLowStock(): boolean {
    return this.currentStock <= this.minStock;
  }

  // Relations
  @ManyToOne(() => PartCategory, { nullable: true })
  @JoinColumn({ name: 'category_id' })
  category?: PartCategory;

  @ManyToOne(() => Supplier, { nullable: true })
  @JoinColumn({ name: 'preferred_supplier_id' })
  preferredSupplier?: Supplier;

  @ManyToOne(() => WarehouseLocation, { nullable: true })
  @JoinColumn({ name: 'location_id' })
  location?: WarehouseLocation;

  // @OneToMany(() => PartAlternate, alt => alt.part)
  // alternates: PartAlternate[];
}
