import { Router, Response } from 'express';
import { requireAuth, AuthRequest } from '../../middleware/auth.middleware';
import { AssociationService } from '../../services/association.service';

const router = Router();
router.use(requireAuth);

/** POST /association/:businessId/donate body { heat_reduction } */
router.post('/:businessId/donate', async (req: AuthRequest, res: Response) => {
  try {
    const { heat_reduction } = req.body || {};
    if (!heat_reduction) return res.status(400).json({ error: 'heat_reduction requis' });
    const r = await AssociationService.donate(req.playerId!, req.params.businessId, Number(heat_reduction));
    res.json(r);
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

/** GET /association/:businessId/can-donate */
router.get('/:businessId/can-donate', async (req: AuthRequest, res: Response) => {
  try {
    const can = await AssociationService.canDonate(req.playerId!, req.params.businessId);
    res.json({ can_donate: can });
  } catch (err: any) { res.status(400).json({ error: err.message }); }
});

export default router;
