// Profile routes
const express = require('express');
const router = express.Router();
const multer = require('multer');
const path = require('path');

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'uploads/');
    },
    filename: (req, file, cb) => {
        cb(null, `${req.user.id}-${Date.now()}${path.extname(file.originalname)}`);
    }
});

const upload = multer({
    storage,
    limits: { fileSize: 5 * 1024 * 1024 }, // 5MB limit
    fileFilter: (req, file, cb) => {
        const filetypes = /jpeg|jpg|png|gif/;
        const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
        const mimetype = filetypes.test(file.mimetype);
        if (mimetype && extname) {
            return cb(null, true);
        } else {
            cb('Error: Images Only!');
        }
    }
});

// Public route (no protect middleware needed, but maybe optional auth to check if user is blocking?)
// For now open to all
const { getProfile, getPublicProfile, uploadProfileImage, upgradeSubscription, cancelSubscription, updateAccountSettings, deleteAccount, getEmailPreferences, updateEmailPreferences, getBadges } = require('../controllers/profileController');
const { protect } = require('../middleware/authMiddleware');

router.get('/public/:username', getPublicProfile);
router.get('/me', protect, getProfile);
router.get('/badges', protect, getBadges);
router.post('/upload', protect, upload.single('image'), uploadProfileImage);
router.post('/upgrade', protect, upgradeSubscription);
router.post('/cancel', protect, cancelSubscription);
router.post('/settings', protect, updateAccountSettings);
router.delete('/', protect, deleteAccount);
router.get('/email-preferences', protect, getEmailPreferences);
router.put('/email-preferences', protect, updateEmailPreferences);

module.exports = router;

