const express = require('express');
const multer = require('multer');
const { body } = require('express-validator');

const settingsController = require('../controllers/profileController');
const { authenticate } = require('../middleware/auth');
const { validate } = require('../middleware/validation');

const router = express.Router();

const upload = multer({
  storage: multer.memoryStorage(),
  limits: {
    fileSize: 5 * 1024 * 1024,
  },
  fileFilter: (req, file, cb) => {
    const allowed = ['image/jpeg', 'image/jpg', 'image/png', 'image/webp'];
    if (!allowed.includes(file.mimetype)) {
      return cb(new Error('Only JPG, PNG, and WEBP images are allowed'));
    }
    cb(null, true);
  },
});

const profileValidation = [
  body('firstName')
    .optional()
    .trim()
    .isLength({ min: 2, max: 50 })
    .withMessage('First name must be 2-50 characters'),

  body('lastName')
    .optional()
    .trim()
    .isLength({ min: 2, max: 50 })
    .withMessage('Last name must be 2-50 characters'),

  body('username')
    .optional()
    .trim()
    .isLength({ min: 3, max: 30 })
    .withMessage('Username must be 3-30 characters')
    .matches(/^[a-zA-Z0-9_]+$/)
    .withMessage('Username can only contain letters, numbers, and underscores'),

  body('email')
    .optional()
    .trim()
    .isEmail()
    .withMessage('Please provide a valid email')
    .normalizeEmail(),

  body('phone')
    .optional()
    .trim()
    .isLength({ min: 5, max: 30 })
    .withMessage('Phone number must be 5-30 characters'),

  body('country')
    .optional()
    .trim()
    .isLength({ min: 2, max: 100 })
    .withMessage('Country is required'),

  body('city')
    .optional()
    .trim()
    .isLength({ min: 2, max: 100 })
    .withMessage('City is required'),

  body('timezone')
    .optional()
    .trim()
    .notEmpty()
    .withMessage('Timezone is required'),

  body('language')
    .optional()
    .trim()
    .notEmpty()
    .withMessage('Language is required'),

  body('currency')
    .optional()
    .trim()
    .notEmpty()
    .withMessage('Currency is required'),

  body('dateFormat')
    .optional()
    .trim()
    .notEmpty()
    .withMessage('Date format is required'),

  body('bio')
    .optional()
    .trim()
    .isLength({ max: 300 })
    .withMessage('Bio must not exceed 300 characters'),

  body('profile').optional().isObject().withMessage('profile must be an object'),
  body('contact').optional().isObject().withMessage('contact must be an object'),
  body('preferences').optional().isObject().withMessage('preferences must be an object'),
  body('location').optional().isObject().withMessage('location must be an object'),
];

router.get('/overview', authenticate, settingsController.getOverview);

router.get('/', authenticate, settingsController.getProfile);

router.put(
  '/update',
  authenticate,
  profileValidation,
  validate,
  settingsController.updateProfile
);

router.post(
  '/avatar',
  authenticate,
  upload.single('avatar'),
  settingsController.uploadAvatar
);

router.delete('/avatar', authenticate, settingsController.deleteAvatar);

module.exports = router;