import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  Index,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { Product } from './product.entity.js';
import { StockQuant } from './stock-quant.entity.js';

@Entity({ schema: 'inventory', name: 'lots' })
@Index('idx_lots_tenant_id', ['tenantId'])
@Index('idx_lots_product_id', ['productId'])
@Index('idx_lots_name_product', ['productId', 'name'], { unique: true })
@Index('idx_lots_expiration_date', ['expirationDate'])
export class Lot {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

  @Column({ type: 'varchar', length: 100, nullable: true })
  ref: string | null;

  @Column({ type: 'date', nullable: true, name: 'manufacture_date' })
  manufactureDate: Date | null;

  @Column({ type: 'date', nullable: true, name: 'expiration_date' })
  expirationDate: Date | null;

  @Column({ type: 'date', nullable: true, name: 'removal_date' })
  removalDate: Date | null;

  @Column({ type: 'date', nullable: true, name: 'alert_date' })
  alertDate: Date | null;

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

  // Relations
  @ManyToOne(() => Product, (product) => product.lots)
  @JoinColumn({ name: 'product_id' })
  product: Product;

  @OneToMany(() => StockQuant, (stockQuant) => stockQuant.lot)
  stockQuants: StockQuant[];

  // Auditoría
  @CreateDateColumn({ name: 'created_at', type: 'timestamp' })
  createdAt: Date;

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