import { Request, Response } from 'express';
import { prisma } from '../models';

const HOTSPOT_MIN = 6;

export const maxPpmController = {
  async findHighest(req: Request, res: Response) {
    const license_id = req.query.license_id as string;
    if (!license_id) {
      res.status(400).json({ success: false, error: 'license_id required' });
      return;
    }
    const top = await prisma.blockModel.findFirst({
      where: { license_id },
      orderBy: { ppm_estimate: 'desc' },
    });
    if (!top) {
      res.status(404).json({ success: false, error: 'No blocks' });
      return;
    }
    res.json({ success: true, data: { block: top } });
  },

  async hotspots(req: Request, res: Response) {
    const license_id = req.query.license_id as string;
    if (!license_id) {
      res.status(400).json({ success: false, error: 'license_id required' });
      return;
    }
    const blocks = await prisma.blockModel.findMany({
      where: { license_id, ppm_estimate: { gte: HOTSPOT_MIN } },
      orderBy: { ppm_estimate: 'desc' },
      take: 500,
    });
    res.json({ success: true, data: { hotspots: blocks, threshold_ppm: HOTSPOT_MIN } });
  },
};
