/**
 * AlmacenProyecto Entity
 * Almacenes por proyecto/obra
 *
 * @module Inventory
 * @table inventory.almacenes_proyecto
 * @ddl schemas/06-inventory-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 { Warehouse } from '../../warehouses/entities/warehouse.entity';

export type WarehouseTypeConstruction = 'central' | 'obra' | 'temporal' | 'transito';

@Entity({ schema: 'inventory', name: 'almacenes_proyecto' })
@Index(['warehouseId'], { unique: true })
export class AlmacenProyecto {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ name: 'warehouse_id', type: 'uuid' })
  warehouseId: string;

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

  @Column({
    name: 'construction_warehouse_type',
    type: 'varchar',
    length: 20,
    default: 'obra',
  })
  constructionWarehouseType: WarehouseTypeConstruction;

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

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

  @Column({ name: 'is_active', type: 'boolean', default: true })
  isActive: boolean;

  @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: 'responsible_id' })
  responsible: User;

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

  // FK a inventory.warehouses (ERP-Core)
  @ManyToOne(() => Warehouse, { nullable: false })
  @JoinColumn({ name: 'warehouse_id' })
  warehouse: Warehouse;
}
