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';
import { StockMove } from './stock-move.entity.js';

export enum PickingType {
  INCOMING = 'incoming',
  OUTGOING = 'outgoing',
  INTERNAL = 'internal',
}

export enum MoveStatus {
  DRAFT = 'draft',
  WAITING = 'waiting',
  CONFIRMED = 'confirmed',
  ASSIGNED = 'assigned',
  DONE = 'done',
  CANCELLED = 'cancelled',
}

@Entity({ schema: 'inventory', name: 'pickings' })
@Index('idx_pickings_tenant_id', ['tenantId'])
@Index('idx_pickings_company_id', ['companyId'])
@Index('idx_pickings_status', ['status'])
@Index('idx_pickings_partner_id', ['partnerId'])
@Index('idx_pickings_scheduled_date', ['scheduledDate'])
export class Picking {
  @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: 100, nullable: false })
  name: string;

  @Column({
    type: 'enum',
    enum: PickingType,
    nullable: false,
    name: 'picking_type',
  })
  pickingType: PickingType;

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

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

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

  @Column({ type: 'timestamp', nullable: true, name: 'scheduled_date' })
  scheduledDate: Date | null;

  @Column({ type: 'timestamp', nullable: true, name: 'date_done' })
  dateDone: Date | null;

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

  @Column({
    type: 'enum',
    enum: MoveStatus,
    default: MoveStatus.DRAFT,
    nullable: false,
  })
  status: MoveStatus;

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

  @Column({ type: 'timestamp', nullable: true, name: 'validated_at' })
  validatedAt: Date | null;

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

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

  @ManyToOne(() => Location)
  @JoinColumn({ name: 'location_id' })
  location: Location;

  @ManyToOne(() => Location)
  @JoinColumn({ name: 'location_dest_id' })
  locationDest: Location;

  @OneToMany(() => StockMove, (stockMove) => stockMove.picking)
  moves: StockMove[];

  // 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;
}
