/**
 * RequisicionLinea Entity
 * Líneas de requisición de obra
 *
 * @module Inventory
 * @table inventory.requisicion_lineas
 * @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 { RequisicionObra } from './requisicion-obra.entity';
import { Concepto } from '../../budgets/entities/concepto.entity';
import { Lote } from '../../construction/entities/lote.entity';
import { Product } from '../../products/entities/product.entity';
import { Uom } from '../../core/entities/uom.entity';

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

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

  @Column({ name: 'requisicion_id', type: 'uuid' })
  requisicionId: string;

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

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

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

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

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

  @Column({
    name: 'quantity_served',
    type: 'decimal',
    precision: 12,
    scale: 4,
    default: 0,
  })
  quantityServed: number;

  @Column({ name: 'unit_id', type: 'uuid', nullable: true })
  unitId: 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;

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

  @ManyToOne(() => RequisicionObra, (req) => req.lineas, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'requisicion_id' })
  requisicion: RequisicionObra;

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

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

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

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

  // FK a core.uom (ERP-Core)
  @ManyToOne(() => Uom, { nullable: true })
  @JoinColumn({ name: 'unit_id' })
  unit: Uom;
}
