const mongoose = require('mongoose')

// const documentSchema = new mongoose.Schema({
//     documentCode: {
//         type: String,
//         required: [true, "Document code is required"],
//         unique: true,
//         trime: true,
//         uppercase: true,
//     },
//     name: {
//         type: String,
//         required: [true, "Document name is required"],
//         trim: true,
//     },
//     category: {
//         type: String,
//         enum: ["Tax", "Legal", "HR", "Compliance", "Finance"],
//         required: true,
//     },
//     description: {
//         type: String,
//         trim: true,
//         default: null,
//     },
//     issueDate: {
//         type: Date,
//         required: true,
//     },

//     expiryDate: {
//         type: Date,
//         default: null, // null = no expiry
//     },
//     status: {
//         type: String,
//         enum: ["Active", "Inactive", "Expired"],
//         default: "Active",
//     },

//     // 🔹 File Storage
//     file: {
//         fileName: { type: String },
//         fileUrl: { type: String }, // S3 / Cloudinary / Local
//         fileType: { type: String }, // pdf, docx, jpg
//         fileSize: { type: Number }, // bytes
//     },

//     // 🔹 Ownership
//     orgId: {
//         type: mongoose.Schema.Types.ObjectId,
//         ref: "Organization",
//         required: true,
//     },

//     // 🔹 Audit Fields
//     createdBy: {
//         type: mongoose.Schema.Types.ObjectId,
//         ref: "User",
//         default: null,
//     },

//     updatedBy: {
//         type: mongoose.Schema.Types.ObjectId,
//         ref: "User",
//         default: null,
//     },

//     // 🔹 Soft Delete
//     isDeleted: {
//         type: Boolean,
//         default: false,
//     },

//     // 🔹 Expiry Automation
//     isExpirable: {
//         type: Boolean,
//         default: true,
//     },

//     notifiedBeforeExpiry: {
//         type: Boolean,
//         default: false,
//     },
// },
    //    policyId: {
    //     type: mongoose.Schema.Types.ObjectId,
    //     ref: "Policy",
    //     default: null,
    //   },
    //   branchId: {
    //     type: mongoose.Schema.Types.ObjectId,
    //     ref: "Branch",
    //     default: null,
    //   },
//     {
//         timestamps: true, // createdAt, updatedAt
//     }
// )
const documentSchema = new mongoose.Schema({
  documentCode: {
    type: String,
    required: true,
    trim: true,
    uppercase: true,
  },
  name: {
    type: String,
    required: true,
    trim: true,
  },
  category: {
    type: String,
    enum: ["Tax", "Legal", "HR", "Compliance", "Finance"],
    required: true,
  },
  description: {
    type: String,
    default: null,
  },
  issueDate: {
    type: Date,
    required: true,
  },
  expiryDate: {
    type: Date,
    default: null,
  },
  status: {
    type: String,
    enum: ["Active", "Inactive", "Expired"],
    default: "Active",
  },
  file: {
    fileName: String,
    fileUrl: String,
    fileType: String,
    fileSize: Number,
  },
  orgId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "Organization",
    required: true,
  },
  createdBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  updatedBy: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
  },
  isDeleted: {
    type: Boolean,
    default: false,
  },
  isExpirable: {
    type: Boolean,
    default: true,
  },
  notifiedBeforeExpiry: {
    type: Boolean,
    default: false,
  },
}, { timestamps: true });

documentSchema.index({ orgId: 1, documentCode: 1 });


module.exports = mongoose.model("Document", documentSchema)