const mongoose = require('mongoose');
const { tenantFields, moneySchema, applyPlatformPlugins } = require('../platformFields');

const schema = new mongoose.Schema({
  ...tenantFields,
  branch: { type: mongoose.Schema.ObjectId, ref: 'Branch', required: true, autopopulate: true },
  cashier: { type: mongoose.Schema.ObjectId, ref: 'Admin', required: true, autopopulate: true },
  openedAt: { type: Date, default: Date.now },
  closedAt: Date,
  openingNote: String,
  closingNote: String,
  status: { type: String, enum: ['open', 'closed'], default: 'open', index: true },
  totals: {
    sales: { type: moneySchema, default: () => ({ amount: 0, currency: 'NGN' }) },
    transactions: { type: Number, default: 0 },
  },
});

module.exports = mongoose.model('PosSession', applyPlatformPlugins(schema));
