/**
 * RequisicionObra Entity
 * Requisiciones de material desde obra
 *
 * @module Inventory
 * @table inventory.requisiciones_obra
 * @ddl schemas/06-inventory-ext-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
  Index,
} from 'typeorm';
import { Tenant } from '../../core/entities/tenant.entity';
import { User } from '../../core/entities/user.entity';
import { Fraccionamiento } from '../../construction/entities/fraccionamiento.entity';
import { RequisicionLinea } from './requisicion-linea.entity';
import { Warehouse } from '../../warehouses/entities/warehouse.entity';

export type RequisitionStatus = 'draft' | 'submitted' | 'approved' | 'partially_served' | 'served' | 'cancelled';

@Entity({ schema: 'inventory', name: 'requisiciones_obra' })
@Index(['tenantId', 'requisitionNumber'], { unique: true })
export class RequisicionObra {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

  @Column({ name: 'requisition_number', type: 'varchar', length: 30 })
  requisitionNumber: string;

  @Column({ name: 'requisition_date', type: 'date' })
  requisitionDate: Date;

  @Column({ name: 'required_date', type: 'date' })
  requiredDate: Date;

  @Column({ type: 'varchar', length: 20, default: 'draft' })
  status: RequisitionStatus;

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

  @Column({ name: 'requested_by', type: 'uuid' })
  requestedById: string;

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

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

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

  @Column({ name: 'rejection_reason', type: 'text', nullable: true })
  rejectionReason: string;

  @Column({ name: 'purchase_order_id', type: 'uuid', nullable: true })
  purchaseOrderId: 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(() => Fraccionamiento)
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'requested_by' })
  requestedBy: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'approved_by' })
  approvedBy: User;

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

  @OneToMany(() => RequisicionLinea, (linea) => linea.requisicion)
  lineas: RequisicionLinea[];

  // FK a inventory.warehouses (ERP-Core) - destino de la requisición
  @ManyToOne(() => Warehouse, { nullable: true })
  @JoinColumn({ name: 'destination_warehouse_id' })
  destinationWarehouse: Warehouse;
}
