// models/BookBorrow.js
const mongoose = require('mongoose');

const bookBorrowSchema = new mongoose.Schema({
  // Book information
  bookId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Book',
    required: true
  },
  bookTitle: {
    type: String,
    required: true
  },
  
  // Borrower information
  borrowerType: {
    type: String,
    enum: ['student', 'teacher'],
    required: true
  },
  borrowerId: {
    type: String,
    required: true
  },
  borrowerName: {
    type: String,
    required: true
  },
  borrowerClass: {
    type: String,
    required: function() {
      return this.borrowerType === 'student';
    }
  },
  borrowerPhone: {
    type: String
  },
  
  // Borrow details
  borrowDate: {
    type: Date,
    default: Date.now
  },
  dueDate: {
    type: Date,
    required: true
  },
  returnDate: {
    type: Date
  },
  
  // Status
  status: {
    type: String,
    enum: ['Borrowed', 'Returned', 'Overdue', 'Lost'],
    default: 'Borrowed'
  },
  
  // Fine
  fine: {
    type: Number,
    default: 0
  },
  finePaid: {
    type: Boolean,
    default: false
  },
  
  // Additional
  remarks: String,
  issuedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Admin'
  }
}, {
  timestamps: true
});

module.exports = mongoose.model('BookBorrow', bookBorrowSchema);