// models/CommandeItem.js (note the capital C and I)
const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');

const CommandeItem = sequelize.define('CommandeItem', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  commande_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'commandes',
      key: 'id'
    }
  },
  menu_id: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  quantite: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 1
  },
  prix_unitaire: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  total_item: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false
  },
  commentaires: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  statut: {
    type: DataTypes.ENUM('commande', 'en_preparation', 'pret', 'servi'),
    allowNull: true,
    defaultValue: 'commande'
  }
}, {
  tableName: 'commande_items',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  hooks: {
    beforeSave: (item) => {
      // Auto-calculate total_item
      item.total_item = parseFloat(item.prix_unitaire) * parseInt(item.quantite);
    }
  }
});

module.exports = CommandeItem;
