const { checkLicense } = require("../modules/license/license.service");

async function licenseMiddleware(req, res, next) {
  try {
    const result = await checkLicense();

    // We can attach license info to the request if needed later
    req.license = result;

    // Block if license is fully expired
    if (result.status === "expired") {
      return res.status(403).json({
        status: "failed",
        error: "License expired. Please renew to continue.",
      });
    }

    next();
  } catch (err) {
    return res.status(401).json({
      status: "failed",
      error: err.message || "License validation failed",
    });
  }
}

module.exports = licenseMiddleware;
