const express = require("express");
const path = require("path");
const fs = require("fs");
const multer = require("multer");
const {
  login,
  logout,
  getMe,
  updateDetails,
  updatePassword,
  forgotPassword,
  resetPassword,
  resendOTP,
  confirmEmail,
} = require("../../controllers/admin/adminAuth");
const router = express.Router();

const { protect, authorize } = require("../../middleware/auth");
const uploadDirectory = "public/uploads/admin/";

// Ensure that the upload directory exists; if not, create it
if (!fs.existsSync(uploadDirectory)) {
  fs.mkdirSync(uploadDirectory, { recursive: true });
}
const allowedExtensions = [".jpg", ".jpeg", ".png", ".gif"];

const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, uploadDirectory);
  },
  filename: function (req, file, cb) {
    cb(
      null,
      file.fieldname +
        "-" +
        req.admin.username +
        "-" +
        Date.now() +
        path.extname(file.originalname)
    );
  },
});

const upload = multer({
  storage: storage,
  fileFilter: (req, file, cb) => {
    const ext = path.extname(file.originalname).toLowerCase();
    if (allowedExtensions.includes(ext)) {
      cb(null, true);
    } else {
      cb(new Error("Invalid file type. Only images are allowed."));
    }
  },
  limits: {
    fileSize: 5 * 1024 * 1024, // 5 MB size limit
  },
});

router.post("/login", login);
router.get("/logout", protect, logout);
router.get("/me", protect, getMe);
router.put(
  "/updatedetails",
  protect,
  upload.single("admin_image_field"),
  updateDetails
);
router.put("/updatepassword", protect, updatePassword);
router.post("/forgotpassword", forgotPassword);
router.put("/resetpassword/", resetPassword);
router.post("/sendotp", protect, resendOTP);
// router.post("/confirmemail", confirmEmail);

module.exports = router;
