<?php

use WHMCS\Module\Addon\creatia_button\Eligibility;
use WHMCS\View\Menu\Item as MenuItem;

add_hook('ClientAreaPrimaryNavbar', 1, function (MenuItem $primaryNavbar) {

    // Guard the autoload: this hook runs on every client area page, so a module that
    // is not installed or not yet autoloadable must not take the navbar down with it.
    if (!class_exists(Eligibility::class)) {
        return;
    }

    $client = Menu::context('client');

    if (is_null($client)) {
        return;
    }

    // Same rule the controller enforces - see Eligibility. Showing the link to
    // someone the controller would turn away, or hiding it from someone it would
    // let through, is how the two drifted apart in the first place.
    if (!Eligibility::clientQualifies((int) $client->id)) {
        return;
    }

    $primaryNavbar->addChild('Creatia Portal')
        ->setUri('index.php?m=creatia_button&action=index')
        ->setOrder(70)
        ->setIcon('fas fa-shield-alt');
});
