# -*- coding: utf-8 -*-
from odoo import http
from odoo.http import request


class NacefControlPlane(http.Controller):
    """Ingest endpoint for tenant fleet-health beacons.

    Tenant databases POST a small JSON status payload here; an optional shared
    token (system parameter ``nacef.beacon_token``) authenticates the sender.
    The payload carries metadata only — no fiscal content.
    """

    @http.route("/nacef/control/beacon", type="json", auth="public",
                methods=["POST"], csrf=False)
    def beacon(self, **payload):
        expected = request.env["ir.config_parameter"].sudo().get_param(
            "nacef.beacon_token")
        if expected and payload.get("token") != expected:
            return {"error": "unauthorized"}
        beacon = request.env["nacef.tenant.beacon"].sudo().ingest(payload)
        return {"ok": True, "beacon_id": beacon.id}
