/**
 * PurchaseOrderConstruction Entity
 * Extensión de órdenes de compra para construcción
 *
 * @module Purchase (MAI-004)
 * @table purchase.purchase_order_construction
 * @ddl schemas/07-purchase-ext-schema-ddl.sql
 */

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  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 { RequisicionObra } from '../../inventory/entities/requisicion-obra.entity';

@Entity({ schema: 'purchase', name: 'purchase_order_construction' })
@Index(['tenantId'])
@Index(['purchaseOrderId'], { unique: true })
@Index(['fraccionamientoId'])
@Index(['requisicionId'])
export class PurchaseOrderConstruction {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  // FK a purchase.purchase_orders (ERP Core)
  @Column({ name: 'purchase_order_id', type: 'uuid' })
  purchaseOrderId: string;

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

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

  // Delivery information
  @Column({ name: 'delivery_location', type: 'varchar', length: 255, nullable: true })
  deliveryLocation: string;

  @Column({ name: 'delivery_contact', type: 'varchar', length: 100, nullable: true })
  deliveryContact: string;

  @Column({ name: 'delivery_phone', type: 'varchar', length: 20, nullable: true })
  deliveryPhone: string;

  // Reception
  @Column({ name: 'received_by', type: 'uuid', nullable: true })
  receivedById: string;

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

  // Quality check
  @Column({ name: 'quality_approved', type: 'boolean', nullable: true })
  qualityApproved: boolean;

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

  // Audit
  @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, { nullable: true })
  @JoinColumn({ name: 'fraccionamiento_id' })
  fraccionamiento: Fraccionamiento;

  @ManyToOne(() => RequisicionObra, { nullable: true })
  @JoinColumn({ name: 'requisicion_id' })
  requisicion: RequisicionObra;

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'received_by' })
  receivedBy: User;

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

  @ManyToOne(() => User, { nullable: true })
  @JoinColumn({ name: 'updated_by' })
  updatedBy: User;
}
