import mongoose from 'mongoose';
import { generateId } from '../../utils.js';
const { Schema } = mongoose;
import { aggregateRollups, aggregateRollupsHistory } from '../../database.js';

const purchaseOrderSchema = new Schema(
  {
    _reference: { type: String, default: () => generateId()() },
    totalAmount: { type: Number, required: true, default: 0 },
    totalAmountWithTax: { type: Number, required: true, default: 0 },
    shippingAmount: { type: Number, required: true, default: 0 },
    shippingAmountWithTax: { type: Number, required: true, default: 0 },
    grandTotalAmount: { type: Number, required: true, default: 0 },
    totalTaxAmount: { type: Number, required: true, default: 0 },
    timestamp: { type: Date, default: Date.now },
    vendor: { type: Schema.Types.ObjectId, ref: 'vendor', required: true },
    state: {
      type: { type: String, required: true, default: 'draft' },
    },
    postedAt: { type: Date, required: false },
    acknowledgedAt: { type: Date, required: false },
    cancelledAt: { type: Date, required: false },
    completedAt: { type: Date, required: false },
  },
  { timestamps: true }
);

const rollupConfigs = [
  {
    name: 'draft',
    filter: { 'state.type': 'draft' },
    rollups: [{ name: 'draft', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'sent',
    filter: { 'state.type': 'sent' },
    rollups: [{ name: 'sent', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'acknowledged',
    filter: { 'state.type': 'acknowledged' },
    rollups: [{ name: 'acknowledged', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'partiallyShipped',
    filter: { 'state.type': 'partiallyShipped' },
    rollups: [{ name: 'partiallyShipped', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'shipped',
    filter: { 'state.type': 'shipped' },
    rollups: [{ name: 'shipped', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'partiallyReceived',
    filter: { 'state.type': 'partiallyReceived' },
    rollups: [{ name: 'partiallyReceived', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'received',
    filter: { 'state.type': 'received' },
    rollups: [{ name: 'received', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'cancelled',
    filter: { 'state.type': 'cancelled' },
    rollups: [{ name: 'cancelled', property: 'state.type', operation: 'count' }],
  },
  {
    name: 'completed',
    filter: { 'state.type': 'completed' },
    rollups: [{ name: 'completed', property: 'state.type', operation: 'count' }],
  },
];

purchaseOrderSchema.statics.stats = async function () {
  const results = await aggregateRollups({
    model: this,
    rollupConfigs: rollupConfigs,
  });

  // Transform the results to match the expected format
  return results;
};

purchaseOrderSchema.statics.history = async function (from, to) {
  const results = await aggregateRollupsHistory({
    model: this,
    startDate: from,
    endDate: to,
    rollupConfigs: rollupConfigs,
  });

  // Return time-series data array
  return results;
};

// Add virtual id getter
purchaseOrderSchema.virtual('id').get(function () {
  return this._id;
});

// Configure JSON serialization to include virtuals
purchaseOrderSchema.set('toJSON', { virtuals: true });

// Create and export the model
export const purchaseOrderModel = mongoose.model('purchaseOrder', purchaseOrderSchema);
