const express = require('express');
const router = express.Router();
const settingsController = require('../controllers/settingsController');
const dashboardRoutes = require('./dashboard');
const dataConvertRoutes = require('./dataConvert');

// Debug endpoint to check configuration
router.get('/config-debug', (req, res) => {
    const config = require('../config');
    res.json({
        success: true,
        config: {
            nocodb: {
                apiUrl: config.nocodb.apiUrl,
                hasToken: !!config.nocodb.apiToken,
                projectId: config.nocodb.projectId,
                tableId: config.nocodb.tableId,
                loginSheetId: config.nocodb.loginSheetId,
                settingsSheetId: config.nocodb.settingsSheetId,
                shiftsSheetId: config.nocodb.shiftsSheetId,
                shiftSignupsSheetId: config.nocodb.shiftSignupsSheetId
            }
        }
    });
});

// Start location management
router.get('/start-location', settingsController.getStartLocation);
router.post('/start-location', settingsController.updateStartLocation);

// Walk sheet configuration
router.get('/walk-sheet-config', settingsController.getWalkSheetConfig);
router.post('/walk-sheet-config', settingsController.updateWalkSheetConfig);

// Dashboard routes
router.use('/dashboard', dashboardRoutes);

// Data convert routes
router.use('/data-convert', dataConvertRoutes);

// Get NocoDB URLs for admin panel
router.get('/nocodb-urls', (req, res) => {
    console.log('Admin NocoDB URLs endpoint called');
    
    // Return the NocoDB URLs directly from environment variables
    // Since this route is protected by requireAdmin middleware, 
    // we know the user is an admin
    const nocodbUrls = {
        viewUrl: process.env.NOCODB_VIEW_URL,
        loginSheet: process.env.NOCODB_LOGIN_SHEET,
        settingsSheet: process.env.NOCODB_SETTINGS_SHEET,
        shiftsSheet: process.env.NOCODB_SHIFTS_SHEET,
        shiftSignupsSheet: process.env.NOCODB_SHIFT_SIGNUPS_SHEET
    };
    
    console.log('Returning NocoDB URLs for admin:', {
        hasViewUrl: !!nocodbUrls.viewUrl,
        hasLoginSheet: !!nocodbUrls.loginSheet,
        hasSettingsSheet: !!nocodbUrls.settingsSheet,
        hasShiftsSheet: !!nocodbUrls.shiftsSheet,
        hasSignupsSheet: !!nocodbUrls.shiftSignupsSheet
    });
    
    res.json({
        success: true,
        nocodbUrls
    });
});

module.exports = router;