/**
 * WorkOrderPart Entity - Partes/Refacciones Utilizadas
 *
 * Partes consumidas en ordenes de trabajo de mantenimiento.
 *
 * @module Assets (MAE-015)
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { WorkOrder } from './work-order.entity';

@Entity('work_order_parts', { schema: 'assets' })
@Index(['workOrderId'])
export class WorkOrderPart {
  @PrimaryGeneratedColumn('uuid')
  id!: string;

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

  // Orden de trabajo
  @Column({ name: 'work_order_id', type: 'uuid' })
  workOrderId!: string;

  @ManyToOne(() => WorkOrder, (wo) => wo.partsUsed, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'work_order_id' })
  workOrder!: WorkOrder;

  // Parte/Refaccion
  @Column({ name: 'part_id', type: 'uuid', nullable: true })
  partId?: string;

  @Column({ name: 'part_code', length: 50, nullable: true })
  partCode?: string;

  @Column({ name: 'part_name', length: 255 })
  partName!: string;

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

  // Cantidades
  @Column({ name: 'quantity_required', type: 'decimal', precision: 10, scale: 2 })
  quantityRequired!: number;

  @Column({ name: 'quantity_used', type: 'decimal', precision: 10, scale: 2, nullable: true })
  quantityUsed?: number;

  // Costos
  @Column({ name: 'unit_cost', type: 'decimal', precision: 18, scale: 2, nullable: true })
  unitCost?: number;

  @Column({ name: 'total_cost', type: 'decimal', precision: 18, scale: 2, nullable: true })
  totalCost?: number;

  // Origen
  @Column({ name: 'from_inventory', type: 'boolean', default: false })
  fromInventory!: boolean;

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

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

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

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

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