<?php

namespace App\Middleware;

use App\Utils\View;

class IntegrationMiddleware
{
    public function handle()
    {
        $headers = getallheaders();
        $tokenHeader = $headers['X-Integration-Token'] ?? $headers['x-integration-token'] ?? $_SERVER['HTTP_X_INTEGRATION_TOKEN'] ?? '';

        $validToken = $_ENV['INTEGRATION_TOKEN'] ?? null;

        if (empty($validToken) || $tokenHeader !== $validToken) {
            View::json(['error' => 'Unauthorized'], 401);
            return false;
        }

        return true;
    }
}
