import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
  Index,
} from 'typeorm';
import { StockTransferLine } from './stock-transfer-line.entity';

export enum TransferStatus {
  DRAFT = 'draft',
  PENDING_APPROVAL = 'pending_approval',
  APPROVED = 'approved',
  IN_TRANSIT = 'in_transit',
  PARTIALLY_RECEIVED = 'partially_received',
  RECEIVED = 'received',
  CANCELLED = 'cancelled',
}

export enum TransferType {
  BRANCH_TO_BRANCH = 'branch_to_branch',
  WAREHOUSE_TO_BRANCH = 'warehouse_to_branch',
  BRANCH_TO_WAREHOUSE = 'branch_to_warehouse',
  INTERNAL = 'internal',
}

@Entity('stock_transfers', { schema: 'retail' })
@Index(['tenantId', 'status', 'createdAt'])
@Index(['tenantId', 'sourceBranchId'])
@Index(['tenantId', 'destBranchId'])
@Index(['tenantId', 'number'], { unique: true })
export class StockTransfer {
  @PrimaryGeneratedColumn('uuid')
  id: string;

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

  @Column({ length: 30, unique: true })
  number: string;

  @Column({
    type: 'enum',
    enum: TransferType,
    default: TransferType.BRANCH_TO_BRANCH,
  })
  type: TransferType;

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

  // Source
  @Column({ name: 'source_branch_id', type: 'uuid', nullable: true })
  sourceBranchId: string;

  @Column({ name: 'source_warehouse_id', type: 'uuid' })
  sourceWarehouseId: string;

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

  // Destination
  @Column({ name: 'dest_branch_id', type: 'uuid', nullable: true })
  destBranchId: string;

  @Column({ name: 'dest_warehouse_id', type: 'uuid' })
  destWarehouseId: string;

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

  // Dates
  @Column({ name: 'requested_date', type: 'date' })
  requestedDate: Date;

  @Column({ name: 'expected_date', type: 'date', nullable: true })
  expectedDate: Date;

  @Column({ name: 'shipped_date', type: 'timestamp with time zone', nullable: true })
  shippedDate: Date;

  @Column({ name: 'received_date', type: 'timestamp with time zone', nullable: true })
  receivedDate: Date;

  // Line counts
  @Column({ name: 'lines_count', type: 'int', default: 0 })
  linesCount: number;

  @Column({ name: 'items_requested', type: 'decimal', precision: 15, scale: 4, default: 0 })
  itemsRequested: number;

  @Column({ name: 'items_shipped', type: 'decimal', precision: 15, scale: 4, default: 0 })
  itemsShipped: number;

  @Column({ name: 'items_received', type: 'decimal', precision: 15, scale: 4, default: 0 })
  itemsReceived: number;

  // Totals (for reporting)
  @Column({ name: 'total_cost', type: 'decimal', precision: 15, scale: 2, default: 0 })
  totalCost: number;

  @Column({ name: 'total_value', type: 'decimal', precision: 15, scale: 2, default: 0 })
  totalValue: number;

  // Shipping info
  @Column({ name: 'shipping_method', length: 50, nullable: true })
  shippingMethod: string;

  @Column({ name: 'tracking_number', length: 100, nullable: true })
  trackingNumber: string;

  @Column({ name: 'carrier_name', length: 100, nullable: true })
  carrierName: string;

  // Users
  @Column({ name: 'requested_by', type: 'uuid' })
  requestedBy: string;

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

  @Column({ name: 'approved_at', type: 'timestamp with time zone', nullable: true })
  approvedAt: Date;

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

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

  // Priority
  @Column({ type: 'int', default: 0 })
  priority: number; // 0=normal, 1=high, 2=urgent

  // Reason
  @Column({ type: 'text', nullable: true })
  reason: string;

  // Reference (for transfers generated from sales, etc.)
  @Column({ name: 'reference_type', length: 50, nullable: true })
  referenceType: string;

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

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

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

  // Metadata
  @Column({ type: 'jsonb', nullable: true })
  metadata: Record<string, any>;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;

  // Relations
  @OneToMany(() => StockTransferLine, (line) => line.transfer)
  lines: StockTransferLine[];
}
