const mongoose = require('mongoose');

// Notice Schema
const NoticeSchema = new mongoose.Schema({
  title: { type: String, required: true },
  content: { type: String, required: true },
  category: { type: String, default: 'General' },
  date: { type: Date, default: Date.now }
});

// Event Schema (News & Events)
const EventSchema = new mongoose.Schema({
  title: { type: String, required: true },
  description: { type: String, required: true },
  category: { type: String, default: 'Event' }, // e.g. Event, News, Sports
  image: { type: String, default: '' }, // base64 or URL
  date: { type: Date, default: Date.now }
});

// Admission Inquiry Schema
const AdmissionInquirySchema = new mongoose.Schema({
  studentName: { type: String, required: true },
  parentName: { type: String, required: true },
  email: { type: String, required: true },
  phone: { type: String, required: true },
  grade: { type: String, required: true },
  message: { type: String, default: '' },
  status: { 
    type: String, 
    enum: ['Pending', 'Contacted', 'Approved', 'Rejected'], 
    default: 'Pending' 
  },
  date: { type: Date, default: Date.now }
});

// Contact Message Schema
const ContactMessageSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true },
  subject: { type: String, required: true },
  message: { type: String, required: true },
  date: { type: Date, default: Date.now }
});

// Faculty Member Schema
const FacultyMemberSchema = new mongoose.Schema({
  name: { type: String, required: true },
  designation: { type: String, required: true },
  department: { type: String, required: true },
  image: { type: String, default: '' }, // base64 or URL
  qualification: { type: String, default: '' }
});

// Gallery Item Schema
const GalleryItemSchema = new mongoose.Schema({
  title: { type: String, default: '' },
  imageUrl: { type: String, required: true }, // base64 or URL
  category: { type: String, default: 'Campus' }, // e.g., Campus, Sports, Classroom, Events
  date: { type: Date, default: Date.now }
});

// Admin Schema
const AdminSchema = new mongoose.Schema({
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true }
});

// Achievement Schema
const AchievementSchema = new mongoose.Schema({
  title: { type: String, required: true },
  recipient: { type: String, required: true },
  category: { type: String, required: true, default: 'Academic' }, // Academic, Sports, Arts, School
  year: { type: String, required: true, default: new Date().getFullYear().toString() },
  featured: { type: Boolean, default: false },
  status: { type: String, enum: ['Published', 'Draft'], default: 'Published' },
  image: { type: String, default: '' }, // base64 or URL
  description: { type: String, default: '' },
  date: { type: Date, default: Date.now }
});

// Settings Schema
const SettingsSchema = new mongoose.Schema({
  schoolName: { type: String, default: "St. Andrews Academy" },
  tagline: { type: String, default: "Where Learning Becomes an Adventure" },
  logo: { type: String, default: "" }, // base64 or URL
  email: { type: String, default: "info@standrews.edu" },
  phone: { type: String, default: "+1 (555) 123-4567" },
  address: { type: String, default: "123 Education Lane, Learning City, LC 12345" },
  facebookUrl: { type: String, default: "https://facebook.com" },
  twitterUrl: { type: String, default: "https://twitter.com" },
  instagramUrl: { type: String, default: "https://instagram.com" },
  youtubeUrl: { type: String, default: "https://youtube.com" }
});

// Student Schema
const StudentSchema = new mongoose.Schema({
  name: { type: String, required: true },
  rollNo: { type: String, required: true, unique: true },
  grade: { type: String, required: true },
  guardianName: { type: String, required: true },
  phone: { type: String, required: true },
  status: { type: String, enum: ['Active', 'Suspended', 'Graduated'], default: 'Active' },
  admissionDate: { type: Date, default: Date.now }
});

// CMS Page Schema
const CMSPageSchema = new mongoose.Schema({
  title: { type: String, required: true },
  slug: { type: String, required: true, unique: true },
  content: { type: String, default: '' },
  status: { type: String, enum: ['Published', 'Draft'], default: 'Draft' },
  lastUpdated: { type: Date, default: Date.now }
});

// Download Schema (Circulars, Syllabus, Forms)
const DownloadSchema = new mongoose.Schema({
  title: { type: String, required: true },
  category: { type: String, default: 'Circular' }, // Circular, Syllabus, Form
  fileUrl: { type: String, required: true }, // Base64 or path
  uploadedBy: { type: String, default: 'Admin' },
  date: { type: Date, default: Date.now }
});

// Activity Log Schema
const ActivityLogSchema = new mongoose.Schema({
  admin: { type: String, default: 'Admin' },
  action: { type: String, required: true },
  details: { type: String, default: '' },
  ipAddress: { type: String, default: '127.0.0.1' },
  date: { type: Date, default: Date.now }
});

// Backup Schema
const BackupSchema = new mongoose.Schema({
  filename: { type: String, required: true },
  size: { type: String, required: true },
  status: { type: String, enum: ['Successful', 'Failed'], default: 'Successful' },
  date: { type: Date, default: Date.now }
});

module.exports = {
  Notice: mongoose.model('Notice', NoticeSchema),
  Event: mongoose.model('Event', EventSchema),
  AdmissionInquiry: mongoose.model('AdmissionInquiry', AdmissionInquirySchema),
  ContactMessage: mongoose.model('ContactMessage', ContactMessageSchema),
  FacultyMember: mongoose.model('FacultyMember', FacultyMemberSchema),
  GalleryItem: mongoose.model('GalleryItem', GalleryItemSchema),
  Admin: mongoose.model('Admin', AdminSchema),
  Achievement: mongoose.model('Achievement', AchievementSchema),
  Settings: mongoose.model('Settings', SettingsSchema),
  Student: mongoose.model('Student', StudentSchema),
  CMSPage: mongoose.model('CMSPage', CMSPageSchema),
  Download: mongoose.model('Download', DownloadSchema),
  ActivityLog: mongoose.model('ActivityLog', ActivityLogSchema),
  Backup: mongoose.model('Backup', BackupSchema)
};
