import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Index,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { Company } from '../../auth/entities/company.entity.js';
import { Location } from './location.entity.js';

@Entity({ schema: 'inventory', name: 'warehouses' })
@Index('idx_warehouses_tenant_id', ['tenantId'])
@Index('idx_warehouses_company_id', ['companyId'])
@Index('idx_warehouses_code_company', ['companyId', 'code'], { unique: true })
export class Warehouse {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

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

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

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

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

  @Column({ type: 'boolean', default: false, nullable: false, name: 'is_default' })
  isDefault: boolean;

  @Column({ type: 'boolean', default: true, nullable: false })
  active: boolean;

  // Relations
  @ManyToOne(() => Company)
  @JoinColumn({ name: 'company_id' })
  company: Company;

  @OneToMany(() => Location, (location) => location.warehouse)
  locations: Location[];

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

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

  @UpdateDateColumn({
    name: 'updated_at',
    type: 'timestamp',
    nullable: true,
  })
  updatedAt: Date | null;

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