const mongoose = require('mongoose');
const { Schema } = mongoose;

// ── User ─────────────────────────────────────────────────────
const UserSchema = new Schema({
  empId:     { type: String, required: true, unique: true, uppercase: true },
  name:      { type: String, required: true },
  email:     { type: String, required: true },
  phone:     { type: String, default: '' },
  dept:      { type: String, default: '' },
  pos:       { type: String, default: '' },
  role:      { type: String, enum: ['admin','employee'], default: 'employee' },
  password:  { type: String, required: true },
  doj:       { type: String, default: '' },
  photo:     { type: String, default: null },
  createdAt: { type: Date, default: Date.now }
});

// ── Leave ────────────────────────────────────────────────────
const LeaveSchema = new Schema({
  empId:     { type: String, required: true, uppercase: true },
  name:      { type: String },
  type:      { type: String, required: true },
  from:      { type: String, required: true },
  to:        { type: String, required: true },
  reason:    { type: String, required: true },
  status:    { type: String, enum: ['pending','approved','rejected'], default: 'pending' },
  appliedOn: { type: String },
  createdAt: { type: Date, default: Date.now }
});

// ── Report ───────────────────────────────────────────────────
const ReportSchema = new Schema({
  empId:         { type: String, required: true },
  name:          { type: String },
  dept:          { type: String },
  pos:           { type: String },
  date:          { type: String },
  clockIn:       { type: String, default: '—' },
  clockOut:      { type: String, default: '—' },
  totalHours:    { type: String, default: '—' },
  tasksDone:     [String],
  tasksProgress: [String],
  remarks:       { type: String, default: '' },
  feedback:      { type: String, default: '' },
  status:        { type: String, default: 'Present' },
  createdAt:     { type: Date, default: Date.now }
});

// ── Message Thread ───────────────────────────────────────────
const MessageSchema = new Schema({
  id:        String,
  from:      String,
  text:      String,
  time:      String,
  date:      String,
  createdAt: { type: Date, default: Date.now }
});
const ThreadSchema = new Schema({
  empId:    { type: String, required: true, unique: true, uppercase: true },
  messages: [MessageSchema]
});

// ── Salary ───────────────────────────────────────────────────
const SalarySchema = new Schema({
  empId:          { type: String, required: true },
  empName:        String,
  dept:           String,
  pos:            String,
  lpa:            Number,
  pfVal:          Schema.Types.Mixed,
  pfType:         String,
  esiVal:         Schema.Types.Mixed,
  esiType:        String,
  ptVal:          Schema.Types.Mixed,
  ptType:         String,
  tdsVal:         Schema.Types.Mixed,
  tdsType:        String,
  leaveDays:      Number,
  leaveDayAmt:    Number,
  special:        Number,
  otherAllowances:[{ label: String, amount: Number }],
  monthly:        Number,
  basic:          Number,
  hra:            Number,
  trans:          Number,
  gross:          Number,
  pfDed:          Number,
  esiDed:         Number,
  ptDed:          Number,
  tdsDed:         Number,
  leaveDed:       Number,
  totalDed:       Number,
  net:            Number,
  periodFrom:     String,
  periodTo:       String,
  notes:          String,
  notified:       { type: Boolean, default: false },
  savedAt:        { type: Date, default: Date.now }
});

// ── Test ─────────────────────────────────────────────────────
const QuestionSchema = new Schema({
  text:    String,
  options: [String],
  correct: Number,
  marks:   { type: Number, default: 1 }
});
const TestSchema = new Schema({
  title:        { type: String, required: true },
  category:     { type: String, default: '' },
  duration:     { type: Number, default: 30 },
  passPct:      { type: Number, default: 60 },
  instructions: { type: String, default: '' },
  questions:    [QuestionSchema],
  assignedTo:   [String],
  accessCode:   { type: String, default: '' },
  codeEnabled:  { type: Boolean, default: false },
  sent:         { type: Boolean, default: false },
  createdAt:    { type: String }
});

// ── Test Result ──────────────────────────────────────────────
const ResultSchema = new Schema({
  testId:      String,
  testTitle:   String,
  empId:       String,
  empName:     String,
  score:       Number,
  totalMarks:  Number,
  pct:         Number,
  passed:      Boolean,
  submittedAt: { type: Date, default: Date.now }
});

module.exports = {
  User:   mongoose.models.User   || mongoose.model('User',   UserSchema),
  Leave:  mongoose.models.Leave  || mongoose.model('Leave',  LeaveSchema),
  Report: mongoose.models.Report || mongoose.model('Report', ReportSchema),
  Thread: mongoose.models.Thread || mongoose.model('Thread', ThreadSchema),
  Salary: mongoose.models.Salary || mongoose.model('Salary', SalarySchema),
  Test:   mongoose.models.Test   || mongoose.model('Test',   TestSchema),
  Result: mongoose.models.Result || mongoose.model('Result', ResultSchema),
};
