const express = require("express");
const { body } = require("express-validator");
const validation = require("../../../../middlewares/routeValidation");
const isAuth = require("../../../../middlewares/authorization");
const jsonParser = require("../../../../helpers/jsonParser");

const {
  accountLogin,
  sellerCompanySignup,
  updateSellerCompanyBasicDetails,
  updateSellerOrBannerImage,
  updateSellerShippingSettings,
  myselfDetails,
  sellerChangePassword,
  getAllSellerAccounts,
  editSellerAccount,
  deleteSellerAccount,
  createSellerAccount,
  getAuditTrail,
} = require("../../../../controllers/seller/accounts");
const { SELLER_SETTINGS_MODULE } = require("../../../../helpers/common");
const checkPermissionMiddleware = require("../../../../middlewares/permission");
const pagination = require("../../../../middlewares/pagination");

const router = express.Router();
router.use(pagination);

router.post(
  "/login",
  [body("username").notEmpty(), body("password").notEmpty()],
  validation,
  accountLogin
);
router.post(
  "/company/register",
  [
    body("company_name").notEmpty(),
    body("company_description").notEmpty(),
    body("address").notEmpty(),
    body("latitude").notEmpty().isFloat(),
    body("longitude").notEmpty().isFloat(),
    body("owner").notEmpty().isString(),
    body("email_address").notEmpty().isEmail(),
    body("phone_number_primary").notEmpty().isLength({ min: 11, max: 12 }),
    body("phone_number_optional_1").optional().isLength({ min: 11, max: 12 }),
    body("phone_number_optional_2").optional().isLength({ min: 11, max: 12 }),
    body("initial_username").notEmpty(),
    body("initial_password").notEmpty(),
  ],
  validation,
  sellerCompanySignup
);
router.patch(
  "/company",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "edit"),
  updateSellerCompanyBasicDetails
);
router.patch(
  "/company/logo",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "edit"),
  updateSellerOrBannerImage
);
router.patch(
  "/company/shipping",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "edit"),
  updateSellerShippingSettings
);
router.get(
  "/me",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "view"),
  myselfDetails
);
router.post(
  "/change-password",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "edit"),
  sellerChangePassword
);
router.get(
  "/user/accounts",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "view"),
  getAllSellerAccounts
);
router.post(
  "/user/accounts",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "add"),
  createSellerAccount
);
router.put(
  "/user/:account_id",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "edit"),
  editSellerAccount
);
router.delete(
  "/user/:account_id",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "delete"),
  deleteSellerAccount
);
router.post(
  "/audit-trail",
  isAuth,
  checkPermissionMiddleware(SELLER_SETTINGS_MODULE, "view"),
  getAuditTrail
);
module.exports = router;
