const express = require('express');
const router = express.Router();
const { getCompanyProfile, updateCompanyProfile } = require('../config/db');

// GET /api/company - Fetch single company profile
router.get('/', async (req, res) => {
  try {
    const company = await getCompanyProfile();
    res.json({ success: true, company });
  } catch (error) {
    console.error('Error fetching company profile:', error);
    res.status(500).json({ success: false, message: 'Failed to fetch company profile' });
  }
});

// PUT /api/company - Update single company profile
router.put('/', async (req, res) => {
  try {
    const updated = await updateCompanyProfile(req.body);
    res.json({ success: true, company: updated, message: 'Company profile updated successfully' });
  } catch (error) {
    console.error('Error updating company profile:', error);
    res.status(500).json({ success: false, message: 'Failed to update company profile', error: error.message });
  }
});

module.exports = router;
