const mongoose = require('mongoose');

const attendanceSchema = new mongoose.Schema({
  date: {
    type: Date,
    required: true
  },
  status: {
    type: String,
    required: true,
    enum: ['Present', 'Absent', 'Late', 'Excused'],
    default: 'Present'
  },
  remarks: {
    type: String,
    trim: true
  }
});

const resultSchema = new mongoose.Schema({
  subject: {
    type: String,
    required: true,
    trim: true
  },
  marks: {
    type: Number,
    required: true,
    min: 0,
    max: 100
  },
  grade: {
    type: String,
    trim: true
  },
  examType: {
    type: String,
    required: true,
    enum: ['Midterm', 'Final', 'Unit Test', 'Quarterly', 'Other']
  },
  examDate: {
    type: Date,
    required: true
  },
  remarks: {
    type: String,
    trim: true
  }
});

const studentSchema = new mongoose.Schema({
  // Personal Information
  firstName: {
    type: String,
    required: true,
    trim: true
  },
  gender: {
    type: String,
    required: true,
    enum: ['Male', 'Female', 'Other']
  },
  dob: {
    type: String,
    required: true
  },
  bloodGroup: {
    type: String,
    required: true,
    enum: ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-']
  },
  religion: {
    type: String,
    required: true,
    enum: ['Hindu', 'Muslim', 'Christian', 'Buddhist', 'Other']
  },
  email: {
    type: String,
    trim: true,
    lowercase: true
  },
  phone: {
    type: String,
    trim: true
  },
  
  // Academic Information
  class: {
    type: String,
    required: true,
    enum: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
  },
  section: {
    type: String,
    required: true,
    enum: ['A', 'B', 'C', 'D', 'Science', 'Commerce', 'Arts']
  },
  roll: {
    type: String,
    trim: true
  },
  schoolName: {
    type: String,
    trim: true
  },
  admissionDate: {
    type: Date
  },
  studentId: {
    type: String,
    trim: true
  },
  admissionId: {
    type: String,
    trim: true
  },
  monthlyFee: {
    type: Number
  },
  password: {
    type: String,
    trim: true
  },
  admissionNo: {
    type: String,
    required: true,
    unique: true,
    trim: true
  },
  // Guardian Information
  fatherName: {
    type: String,
    trim: true
  },
  motherName: {
    type: String,
    trim: true
  },
  guardianName: {
    type: String,
    trim: true
  },
  guardianRelation: {
    type: String,
    enum: ['Father', 'Mother', 'Other', '']
  },
  guardianProfession: {
    type: String,
    trim: true
  },
  guardianPhone: {  // Added guardian phone number field
    type: String,
    trim: true
  },
  
  // Attendance Information
  attendance: [attendanceSchema],
  
  // Result Information
  results: [resultSchema],
  
  // System Fields
  photo: {
    type: String, // URL or file path to photo
    default: null
  },
  status: {
    type: String,
    enum: ['active', 'inactive', 'pending'],
    default: 'active'
  },
  createdAt: {
    type: Date,
    default: Date.now
  },
  updatedAt: {
    type: Date,
    default: Date.now
  }
});

// Update the updatedAt field before saving
studentSchema.pre('save', function(next) {
  this.updatedAt = Date.now();
  next();
});

module.exports = mongoose.model('Student', studentSchema);