/**
 * Awards & Recognition Routes
 * Handles CRUD operations for awards, certifications, recognitions, and milestones
 */

import { Router, Request, Response } from 'express';
import { getModels } from '../models';

const router = Router();

// ============================================
// GET ALL AWARDS
// ============================================
router.get('/:companyId', async (req: Request, res: Response) => {
  try {
    const { companyId } = req.params;
    const { Award } = getModels();

    const awards = await Award.find({ companyId }).sort({ awardDate: -1 });
    res.json(awards);
  } catch (error) {
    console.error('Error fetching awards:', error);
    res.status(500).json({ error: 'Failed to fetch awards' });
  }
});

// ============================================
// GET SINGLE AWARD
// ============================================
router.get('/detail/:id', async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { Award } = getModels();

    const award = await Award.findById(id);
    if (!award) {
      return res.status(404).json({ error: 'Award not found' });
    }
    res.json(award);
  } catch (error) {
    console.error('Error fetching award:', error);
    res.status(500).json({ error: 'Failed to fetch award' });
  }
});

// ============================================
// CREATE AWARD
// ============================================
router.post('/', async (req: Request, res: Response) => {
  try {
    const { Award } = getModels();
    const award = new Award(req.body);
    await award.save();
    res.status(201).json(award);
  } catch (error) {
    console.error('Error creating award:', error);
    res.status(500).json({ error: 'Failed to create award' });
  }
});

// ============================================
// UPDATE AWARD
// ============================================
router.put('/:id', async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { Award } = getModels();

    const award = await Award.findByIdAndUpdate(
      id,
      { ...req.body, updatedAt: new Date() },
      { new: true }
    );

    if (!award) {
      return res.status(404).json({ error: 'Award not found' });
    }
    res.json(award);
  } catch (error) {
    console.error('Error updating award:', error);
    res.status(500).json({ error: 'Failed to update award' });
  }
});

// ============================================
// DELETE AWARD
// ============================================
router.delete('/:id', async (req: Request, res: Response) => {
  try {
    const { id } = req.params;
    const { Award } = getModels();

    const award = await Award.findByIdAndDelete(id);
    if (!award) {
      return res.status(404).json({ error: 'Award not found' });
    }
    res.json({ message: 'Award deleted successfully' });
  } catch (error) {
    console.error('Error deleting award:', error);
    res.status(500).json({ error: 'Failed to delete award' });
  }
});

export default router;