const asyncHandler = require('../middlewares/asyncHandler');
const {
  getAllLocations,
  getLocationById,
  getLocationsByCountryId,
  createLocation,
  updateLocation,
  deleteLocation,
  getLocationDepartments,
  addDepartmentToLocation,
  removeDepartmentFromLocation,
  getAllLocationDepartmentPairs
} = require('../repositories/locationsRepository');

// Get all locations
exports.getAllLocations = asyncHandler(async (req, res) => {
    const rows = await getAllLocations();
    res.status(200).json({ success: true, data: rows });
});

// Get location by ID
exports.getLocationById = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const location = await getLocationById(id);
    
    if (!location) {
        return res.status(404).json({ success: false, message: 'Location not found' });
    }
    
    res.status(200).json({ success: true, data: location });
});

// Get locations by country ID
exports.getLocationsByCountryId = asyncHandler(async (req, res) => {
    const { country_id } = req.params;
    const rows = await getLocationsByCountryId(country_id);
    res.status(200).json({ success: true, data: rows });
});

// Create location (site)
exports.createLocation = asyncHandler(async (req, res) => {
    const { location_id, country_id, location_name } = req.body;
    
    if (!country_id || !location_name) {
        return res.status(400).json({ success: false, message: 'country_id and location_name are required' });
    }
    
    const id = await createLocation(country_id, location_name, location_id);
    res.status(201).json({ success: true, message: 'Location (site) created successfully', data: { location_id: id } });
});

// Update location
exports.updateLocation = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const { country_id, location_name } = req.body;
    
    if (!location_name) {
        return res.status(400).json({ success: false, message: 'location_name is required' });
    }
    
    const updated = await updateLocation(id, country_id, location_name);
    
    if (!updated) {
        return res.status(404).json({ success: false, message: 'Location not found' });
    }
    
    res.status(200).json({ success: true, message: 'Location updated successfully' });
});

// Delete location
exports.deleteLocation = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const deleted = await deleteLocation(id);
    
    if (!deleted) {
        return res.status(404).json({ success: false, message: 'Location not found' });
    }
    
    res.status(200).json({ success: true, message: 'Location deleted successfully' });
});

// Get departments for a location
exports.getLocationDepartments = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const rows = await getLocationDepartments(id);
    res.status(200).json({ success: true, data: rows });
});

// Add department to location
exports.addDepartmentToLocation = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const { department_id } = req.body;
    
    if (!department_id) {
        return res.status(400).json({ success: false, message: 'department_id is required' });
    }
    
    await addDepartmentToLocation(id, department_id);
    res.status(201).json({ success: true, message: 'Department added to location successfully' });
});

// Remove department from location
exports.removeDepartmentFromLocation = asyncHandler(async (req, res) => {
    const { id } = req.params;
    const { department_id } = req.body;
    
    if (!department_id) {
        return res.status(400).json({ success: false, message: 'department_id is required' });
    }
    
    const removed = await removeDepartmentFromLocation(id, department_id);
    
    if (!removed) {
        return res.status(404).json({ success: false, message: 'Department-location relationship not found' });
    }
    
    res.status(200).json({ success: true, message: 'Department removed from location successfully' });
});

// Get all location-department pairs (sites with work areas)
exports.getLocationDepartmentPairs = asyncHandler(async (req, res) => {
    const rows = await getAllLocationDepartmentPairs();
    res.status(200).json({ success: true, data: rows });
});

