import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { Document, Types } from "mongoose";

export type StockMovementSheetDocument = StockMovementSheet & Document;

/**
 * One row in the Strategic Stock Movement Sheet.
 * Item descriptor fields are resolved from the Item master on save.
 * warehouseBags maps warehouse ID → bag count.
 */
@Schema({ _id: false })
export class StockSheetLineItem {
  @Prop({ required: true }) declare lineNumber: number;

  // Item master reference
  @Prop({ type: Types.ObjectId, ref: "Item", required: true }) declare itemId: Types.ObjectId;

  // Resolved from Item master (stored for display / PDF)
  @Prop({ type: String, default: null }) declare itemCode: string | null;
  @Prop({ type: String, default: null }) declare barcode: string | null;
  @Prop({ type: String, default: null }) declare itemName: string | null;
  @Prop({ type: String, default: null }) declare blend: string | null;
  @Prop({ type: String, default: null }) declare grainType: string | null;
  @Prop({ type: String, default: null }) declare varietyType: string | null;
  @Prop({ type: String, default: null }) declare processType: string | null;
  @Prop({ type: String, default: null }) declare coo: string | null;
  @Prop({ type: String, default: null }) declare unit: string | null;

  // User-entered fields
  @Prop({ required: true }) declare batchNumber: string;
  @Prop({ type: String, default: null }) declare productionDate: string | null;
  @Prop({ required: true }) declare expiryDate: string;
  @Prop({ type: String, default: null }) declare shipmentNo: string | null;
  @Prop({ type: String, default: null }) declare qualityReportNo: string | null;

  /**
   * Per-warehouse bag counts: { "<warehouseId>": <bags> }
   * Stored as warehouse IDs for reliable inventory updates.
   * The frontend sends warehouse IDs; display names are resolved on read.
   */
  @Prop({ type: Object, default: {} }) declare warehouseBags: Record<string, number>;

  // Auto-calculated
  @Prop({ type: Number, default: 0 }) declare totalBags: number;
  @Prop({ type: Number, default: 0 }) declare totalMt: number;
}

const StockSheetLineItemSchema = SchemaFactory.createForClass(StockSheetLineItem);

/**
 * Strategic Stock Movement Sheet — manually entered, mirrors the PDF layout.
 * Submitted with pending_approval; inventory updated only on approval.
 */
@Schema({ collection: "stock_movement_sheets", timestamps: false })
export class StockMovementSheet {
  // Sheet-level header fields
  @Prop({ type: String, default: null }) declare companyName: string | null;
  @Prop({ type: String, default: null }) declare reportTitle: string | null;
  @Prop({ type: String, default: null }) declare stockReportFor: string | null;
  @Prop({ type: String, default: null }) declare documentDate: string | null;
  @Prop({ type: String, default: null }) declare documentNumber: string | null;
  @Prop({ type: String, default: null }) declare referenceNumber: string | null;
  @Prop({ type: String, default: null }) declare preparedBy: string | null;
  @Prop({ type: String, default: null }) declare reviewedBy: string | null;
  @Prop({ type: String, default: null }) declare approvedByField: string | null;

  // Line items
  @Prop({ type: [StockSheetLineItemSchema], default: [] }) declare lineItems: StockSheetLineItem[];

  // Sheet-level totals (auto-computed on save)
  @Prop({ type: Number, default: 0 }) declare totalBagsAll: number;
  @Prop({ type: Number, default: 0 }) declare totalMtAll: number;

  // Approval workflow
  @Prop({
    required: true,
    enum: ["pending_approval", "approved", "rejected"],
    default: "pending_approval",
  })
  declare approvalStatus: string;

  @Prop({ type: String, default: null }) declare approvedBy: string | null;
  @Prop({ type: String, default: null }) declare approvedAt: string | null;
  @Prop({ type: String, default: null }) declare rejectedBy: string | null;
  @Prop({ type: String, default: null }) declare rejectedAt: string | null;
  @Prop({ type: String, default: null }) declare rejectedReason: string | null;
  @Prop({ type: String, default: null }) declare submittedBy: string | null;

  /** Reference to the linked StockInvoice document (if uploaded via Royal AI) */
  @Prop({ type: Types.ObjectId, ref: "StockInvoice", default: null })
  declare invoiceId: Types.ObjectId | null;

  @Prop({ required: true }) declare createdAt: string;
}

export const StockMovementSheetSchema = SchemaFactory.createForClass(StockMovementSheet);
StockMovementSheetSchema.index({ approvalStatus: 1 });
