const mongoose = require('mongoose').set('debug', false);
const Schema = mongoose.Schema;

const shared_consts = require('../tools/shared_nodejs');

const { ObjectId } = require('mongodb');

const Scontistica = require('../models/scontistica');
const OrderClass = require('../modules/OrderClass');

mongoose.Promise = global.Promise;
mongoose.level = 'F';

const fs = require('fs'); // 👈 Usa il modulo promises

// Resolving error Unknown modifier: $pushAll
mongoose.plugin((schema) => {
  schema.options.usePushEach = true;
});

const orderSchema = new Schema({
  idapp: {
    type: String,
  },
  userId: { type: Schema.Types.ObjectId, ref: 'User' },
  status: {
    type: Number,
    index: true,
  },
  idProduct: { type: Schema.Types.ObjectId, ref: 'Product', index: true },
  idProducer: { type: Schema.Types.ObjectId, ref: 'Producer' },
  idStorehouse: { type: Schema.Types.ObjectId, ref: 'StoreHouse' },
  idScontisticas: [{ type: Schema.Types.ObjectId, ref: 'Scontistica' }],
  idProvider: { type: Schema.Types.ObjectId, ref: 'Provider' },
  idGasordine: { type: Schema.Types.ObjectId, ref: 'Gasordine' },
  price: {
    type: Number,
    default: 0,
  },
  after_price: {
    type: String,
  },
  color: {
    type: String,
  },
  size: {
    type: String,
  },
  quantity: {
    type: Number,
    default: 0,
  },
  quantitypreordered: {
    type: Number,
    default: 0,
  },
  TotalPriceProduct: {
    type: Number,
    default: 0,
  },
  TotalPriceProductstr: {
    type: String,
  },
  TotalPriceProductCalc: {
    type: Number,
    default: 0,
  },
  confermato: {
    // e quindi è stato tolto dal magazzino (aggiornando il campo stockQty)
    type: Boolean,
    default: false,
  },
  date_confermato: {
    type: Date,
  },
  pagato: {
    type: Boolean,
    default: false,
  },
  date_pagato: {
    type: Date,
  },
  consegnato: {
    type: Boolean,
    default: false,
  },
  date_consegnato: {
    type: Date,
  },
  spedito: {
    type: Boolean,
    default: false,
  },
  date_spedito: {
    type: Date,
  },
  ricevuto: {
    type: Boolean,
    default: false,
  },
  date_ricevuto: {
    type: Date,
  },
  weight: {
    type: Number,
  },
  unit: {
    type: Number,
  },
  stars: {
    type: Number,
  },
  date_created: {
    type: Date,
  },
  date_checkout: {
    type: Date,
  },
  date_payment: {
    type: Date,
  },
  date_shipping: {
    type: Date,
  },
  date_delivered: {
    type: Date,
  },
  note: {
    type: String,
  },
  codice_sconto: {
    type: String,
  },
  modify_at: {
    type: Date,
    index: true,
  },
});

var Order = (module.exports = mongoose.model('Order', orderSchema));

module.exports
  .createIndexes()
  .then(() => {})
  .catch((err) => {
    throw err;
  });

module.exports.getFieldsForSearch = function () {
  return [];
};

module.exports.executeQueryTable = function (idapp, params) {
  const tools = require('../tools/general');

  params.fieldsearch = this.getFieldsForSearch();
  return tools.executeQueryTable(this, idapp, params);
};

module.exports.findAllIdApp = async function (idapp) {
  const query = [
    { $match: { idapp } },
    {
      $lookup: {
        from: 'products',
        localField: 'idProduct',
        foreignField: '_id',
        as: 'product',
      },
    },
    {
      $lookup: {
        from: 'producers',
        localField: 'product.idProducer',
        foreignField: '_id',
        as: 'producer',
      },
    },
    {
      $lookup: {
        from: 'providers',
        localField: 'product.idProvider',
        foreignField: '_id',
        as: 'provider',
      },
    },
    {
      $lookup: {
        from: 'gasordines',
        localField: 'idGasordine',
        foreignField: '_id',
        as: 'gasordine',
      },
    },
    {
      $unwind: {
        path: '$gasordine',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $match: {
        $or: [{ gasordine: { $exists: false } }, { 'gasordine.active': true }],
      },
    },
    {
      $lookup: {
        from: 'scontisticas',
        localField: 'product.idScontisticas',
        foreignField: '_id',
        as: 'scontistica',
      },
    },
    {
      $unwind: {
        path: '$product',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $unwind: {
        path: '$producer',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $unwind: {
        path: '$provider',
        preserveNullAndEmptyArrays: true,
      },
    },
  ];

  return await Order.aggregate(query);
};

module.exports.getAllOrders = function (query, sort, callback) {
  Order.find(query, null, sort, callback);
};

module.exports.getOrderByUserId = function (userId, sort, callback) {
  Order.find({ userId }, null, sort, callback);
};

module.exports.getOrderByID = function (id, callback) {
  Order.findById(id, callback);
};

module.exports.createOrder = async function (order, codice_sconto) {
  try {
    if (order.idGasordine === '') {
      order.idGasordine = undefined;
    }
    await Order.updateTotals(order, codice_sconto);
    return await Order.create(order).then((ris) => {
      if (!!ris) return ris._id;
      return null;
    });
  } catch (e) {
    console.error('err', e);
  }
};

module.exports.updateStatusOrders = async function (arrOrders, status) {
  for (const order of arrOrders) {
    let ret = await Order.updateOne({ _id: order.order._id }, { $set: { status } });
  }
};

module.exports.updateStatusOrdersElements = async function (arrOrders, myelements) {
  for (const order of arrOrders) {
    const ret = await Order.findOneAndUpdate({ _id: order.order._id }, { $set: myelements });
  }
};

module.exports.updateOrderByParams = async function (idOrder, paramstoupdate) {
  const ris = await Order.findOneAndUpdate({ _id: idOrder }, { $set: paramstoupdate });

  let myorder = await Order.findOne({ _id: idOrder }).lean();

  if (paramstoupdate && !paramstoupdate.hasOwnProperty('TotalPriceProduct')) {
    await this.updateTotals(myorder);

    await Order.findOneAndUpdate({ _id: idOrder }, { $set: myorder });
  }

  return myorder;
};

module.exports.updateTotals = async function (order, codice_sconto) {
  try {
    const CartClass = require('../modules/Cart');

    if (!order) return;

    OrderClass.initOrderTotals(order);

    let total = 0;

    let scontoapplicato = false;

    let recscontisticheTrovate = [];

    if (codice_sconto) {
      recscontisticheTrovate = await CartClass.getRecSconto(order.idapp, codice_sconto, true);
    }

    // Se ha inserito una scontistica che esiste...
    if (recscontisticheTrovate && recscontisticheTrovate.length > 0) {
      const { sconti_da_applicare, qtanonscontata } = OrderClass.applyNonCumulativeDiscounts(order, recscontisticheTrovate);
      total = OrderClass.calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata);
    } else if (order.scontisticas && order.scontisticas.length > 0) {
      const { sconti_da_applicare, qtanonscontata } = OrderClass.applyNonCumulativeDiscounts(order, order.scontisticas);
      total = OrderClass.calculateDiscountedPrice(order, sconti_da_applicare, qtanonscontata);
    } else {
      total = OrderClass.calculateFullPrice(order);
    }

    order.TotalPriceProductCalc += total;
    order.TotalPriceProduct += total;
    order.TotalPriceProductstr = parseFloat(order.TotalPriceProduct.toFixed(2));
    order.codice_sconto = codice_sconto;

    return order;
  } catch (e) {
    console.error('Err:', e);
  }
};

module.exports.getTotalOrderById = async function (id) {
  const query = [
    { $match: { _id: new ObjectId(id) } },
    {
      $lookup: {
        from: 'products',
        localField: 'idProduct',
        foreignField: '_id',
        as: 'product',
      },
    },
    {
      $unwind: {
        path: '$product',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $lookup: {
        from: 'producers',
        localField: 'product.idProducer',
        foreignField: '_id',
        as: 'producer',
      },
    },
    {
      $unwind: {
        path: '$producer',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $lookup: {
        from: 'storehouses',
        localField: 'idStorehouse',
        foreignField: '_id',
        as: 'storehouse',
      },
    },
    {
      $unwind: {
        path: '$storehouse',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $lookup: {
        from: 'providers',
        localField: 'product.idProvider',
        foreignField: '_id',
        as: 'provider',
      },
    },
    {
      $unwind: {
        path: '$provider',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $lookup: {
        from: 'gasordines',
        localField: 'idGasordine',
        foreignField: '_id',
        as: 'gasordine',
      },
    },
    {
      $unwind: {
        path: '$gasordine',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $match: {
        $or: [{ gasordine: { $exists: false } }, { 'gasordine.active': true }],
      },
    },
    {
      $lookup: {
        from: 'scontisticas',
        localField: 'product.idScontisticas',
        foreignField: '_id',
        as: 'scontisticas',
      },
    },
    {
      $lookup: {
        from: 'orders',
        let: { productId: '$product._id' },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $eq: ['$idProduct', '$$productId'] },
                  {
                    $or: [
                      {
                        $eq: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT],
                      },
                      {
                        $and: [
                          { $lt: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT] },
                          {
                            $gt: [
                              '$modify_at',
                              { $subtract: [new Date(), 60 * 60 * 1000] }, // 1 hour in milliseconds 60 * 60
                            ],
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            },
          },
          {
            $group: {
              _id: null,
              totalQty: { $sum: '$quantity' },
            },
          },
        ],
        as: 'productOrders',
      },
    },
    {
      $lookup: {
        from: 'orders',
        let: { productId: '$product._id' },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [
                  { $eq: ['$idProduct', '$$productId'] },
                  {
                    $or: [
                      {
                        $eq: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT],
                      },
                      {
                        $and: [
                          { $lt: ['$status', shared_consts.OrderStatus.CHECKOUT_SENT] },
                          {
                            $gt: [
                              '$modify_at',
                              { $subtract: [new Date(), 60 * 60 * 1000] }, // 1 hour in milliseconds 60 * 60
                            ],
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
            },
          },
          {
            $group: {
              _id: null,
              totalQtyPreordered: { $sum: '$quantitypreordered' },
            },
          },
        ],
        as: 'productPreOrders',
      },
    },
    {
      $addFields: {
        'product.QuantitaOrdinateInAttesa': {
          $ifNull: [
            {
              $cond: {
                if: { $isArray: '$productOrders' },
                then: { $arrayElemAt: ['$productOrders.totalQty', 0] },
                else: 0,
              },
            },
            0,
          ],
        },
        'product.QuantitaPrenotateInAttesa': {
          $ifNull: [
            {
              $cond: {
                if: { $isArray: '$productPreOrders' },
                then: { $arrayElemAt: ['$productPreOrders.totalQtyPreordered', 0] },
                else: 0,
              },
            },
            0,
          ],
        },
      },
    },
    {
      $addFields: {
        'product.quantityAvailable': {
          $subtract: ['$product.stockQty', '$product.QuantitaOrdinateInAttesa'],
        },
        'product.bookableAvailableQty': {
          $subtract: ['$product.maxbookableGASQty', '$product.QuantitaPrenotateInAttesa'],
        },
      },
    },
    {
      $unset: 'productOrders',
    },
    {
      $unset: 'productPreOrders',
    },
  ];


  
  const ris = await Order.aggregate(query);
  return ris;
};

module.exports.deleteOrderById = async function(id) {
  try {
    
    const ris = await Order.findOneAndDelete({ _id: id });
    return ris;
  } catch (e) {
    console.error('Err', e);
    return null;
  }
}


module.exports.RemoveDeletedOrdersInOrderscart = async function () {
  try {
    const OrdersCart = require('./orderscart');

    // Cancella gli Ordini che non esistono in OrdersCart
    const arrorders = await OrdersCart.find({}).lean();
    for (const rec of arrorders) {
      let recordercart = await OrdersCart.getOrdersCartById(rec._id);

      let arrord = [];
      let cambiare = false;
      for (const recOrd of recordercart.items) {
        if (recOrd.order) {
          arrord.push(recOrd);
        } else {
          cambiare = true;
        }
      }
      if (cambiare) {
        await OrdersCart.findOneAndUpdate({ _id: recordercart._id }, { $set: { items: arrord } }, { new: false });
      }
    }

    // Controllo se Order non esiste in OrdersCart
    const arrord = await Order.find({}).lean();
    for (const ord of arrord) {
      const idtofind = ord._id;

      await OrdersCart.findOne({ 'items.order': { $in: [idtofind] } })
        .then(async (orderCart) => {
          if (!orderCart) {
            // NON TROVATO ! Allora lo cancello

            await Order.findOneAndDelete({ _id: ord._id });
          }
        })
        .catch((err) => console.error(err));
    }
  } catch (e) {
    console.error('Err', e);
  }
};

module.exports.GeneraCSVOrdineProdotti = async function () {
  const myidGasordine = '65c2a8cc379ee4f57e865ee7';

  const myquery = [
    { $match: { idGasordine: new ObjectId(myidGasordine) } },
    {
      $lookup: {
        from: 'products',
        localField: 'idProduct',
        foreignField: '_id',
        as: 'product',
      },
    },
    {
      $unwind: {
        path: '$product',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $lookup: {
        from: 'gasordines',
        localField: 'idGasordine',
        foreignField: '_id',
        as: 'gasordine',
      },
    },
    {
      $unwind: {
        path: '$gasordine',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $match: {
        $or: [
          { 'gasordine.active': true }, // Include documents where gasordines.active is true
          { gasordine: { $exists: false } }, // Include documents where gasordines array doesn't exist
        ],
      },
    },

    {
      $match: {
        $or: [{ quantity: { $gt: 0 } }, { quantitypreordered: { $gt: 0 } }],
      },
    },

    {
      $group: {
        _id: '$product._id',
        name: { $first: '$product.productInfo.name' },
        weight: { $first: '$product.productInfo.weight' },
        price_acquistato: { $first: '$product.price_acquistato' },
        totalQuantity: { $sum: { $add: ['$quantity', '$quantitypreordered'] } },
        totalPrice_acquistato: {
          $sum: { $multiply: ['$product.price_acquistato', { $add: ['$quantity', '$quantitypreordered'] }] },
        },
        count: { $sum: 1 },
      },
    },
    {
      $sort: {
        name: 1, // Sort in ascending order based on the "date_created" field
      },
    },
  ];

  let myorderscart = await Order.aggregate(myquery);

  console.log(myorderscart);

  return generateCSV(myorderscart, 'outout.csv');
};

function generateCSV(data, outputPath) {
  const headers = ['Num', 'Nome', 'Peso', 'Prezzo', 'Quantita', 'Totale', 'Ordini'];

  const rows = data.map((item) => {
    const formattedPrice = item.price_acquistato.toString().replace(/\./g, ','); // Converti "." in ","
    const total = item.totalPrice_acquistato.toString().replace(/\./g, ','); // Converti "." in ","
    return [0, `"${item.name}"`, item.weight, formattedPrice, item.totalQuantity, total, item.count];
  });

  rows.unshift(headers);

  const csvData = rows.map((row) => row.join('|'));

  fs.writeFile(outputPath, csvData.join('\n'), (err) => {
    if (err) {
      console.error('Error writing CSV file:', err);
    } else {
      console.log('CSV file has been successfully generated:', outputPath);
    }
  });
}

// const Order = mongoose.model('Order', OrderSchema);

// module.exports = { Order };


// **** TOTALPRICE !!!