/**
 * ConsumoObra Entity
 * Consumos de materiales por obra/lote
 *
 * @module Inventory
 * @table inventory.consumos_obra
 * @ddl schemas/06-inventory-ext-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { Fraccionamiento } from '../../construction/entities/fraccionamiento.entity';
import { Lote } from '../../construction/entities/lote.entity';
import { Concepto } from '../../budgets/entities/concepto.entity';
import { Product } from '../../products/entities/product.entity';

@Entity({ schema: 'inventory', name: 'consumos_obra' })
export class ConsumoObra {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ name: 'fraccionamiento_id', type: 'uuid' })
  fraccionamientoId: string;

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

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

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

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

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

  @Column({ name: 'unit_cost', type: 'decimal', precision: 12, scale: 4, nullable: true })
  unitCost: number;

  @Column({ name: 'consumption_date', type: 'date' })
  consumptionDate: Date;

  @Column({ name: 'registered_by', type: 'uuid' })
  registeredById: string;

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

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

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

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

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

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

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

  // Computed property (in DB is GENERATED ALWAYS AS)
  get totalCost(): number {
    return this.quantity * (this.unitCost || 0);
  }

  // Relations
  @ManyToOne(() => Tenant)
  @JoinColumn({ name: 'tenant_id' })
  tenant: Tenant;

  @ManyToOne(() => Fraccionamiento)
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @ManyToOne(() => Lote)
  @JoinColumn({ name: 'lote_id' })
  lote: Lote;

  @ManyToOne(() => Concepto)
  @JoinColumn({ name: 'concepto_id' })
  concepto: Concepto;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'registered_by' })
  registeredBy: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'created_by' })
  createdBy: User;

  // FK a products.products (ERP-Core)
  @ManyToOne(() => Product, { nullable: false })
  @JoinColumn({ name: 'product_id' })
  product: Product;

  // NOTA: stockMoveId no tiene relación aún - StockMove entity pendiente de crear
}
