<?php

namespace WHMCS\Module\Addon\creatia_button;

use WHMCS\Database\Capsule;

/**
 * Who is allowed a Creatia account.
 *
 * The navbar hook and the client area controller have to agree on this. When they
 * did not, hiding the menu item enforced nothing - index.php?m=creatia_button was
 * reachable directly, so the "must hold a service on this server" setting was
 * decorative and any client with any active service could provision.
 */
class Eligibility {

    /**
     * Read every setting for this module in one query.
     *
     * @return array<string,string>
     */
    public static function settings(): array {
        return Capsule::table('tbladdonmodules')
            ->where('module', 'creatia_button')
            ->pluck('value', 'setting')
            ->all();
    }

    /**
     * Whether the client holds any active service at all.
     *
     * Only used to tell "you have nothing with us" apart from "what you have is not on
     * the right server", so the page can say which.
     *
     * @param int $clientId
     * @return bool
     */
    public static function hasAnyActiveService(int $clientId): bool {
        return Capsule::table('tblhosting')
            ->where('userid', $clientId)
            ->where('domainstatus', 'Active')
            ->exists();
    }

    /**
     * Whether a client may provision a Creatia account.
     *
     * An unconfigured module qualifies nobody. The settings rows only exist once an
     * administrator has saved the addon configuration, and until they have there is
     * no way to tell whether "any service" was intended - so the safe reading of an
     * absent setting is that nothing is permitted yet, not that everything is.
     *
     * @param int $clientId
     * @param array<string,string>|null $settings Pass the result of settings() to avoid a second query.
     *
     * @return bool
     */
    public static function clientQualifies(int $clientId, ?array $settings = null): bool {

        if ($clientId <= 0) {
            return false;
        }

        $settings = $settings ?? self::settings();

        // The dropdown is declared with an array of Options, so WHMCS stores its key:
        // '0' for any product, '1' for a service on the nominated server.
        $requirement = (string) ($settings['any_product'] ?? '');

        $query = Capsule::table('tblhosting')
            ->where('userid', $clientId)
            ->where('domainstatus', 'Active');

        if ($requirement === '0') {
            return $query->exists();
        }

        if ($requirement === '1') {

            $targetServerId = $settings['target_server_id'] ?? '';

            // '0' is not a server ID - tblservers is auto-increment from 1 - and matching
            // on it would select every service with no server assigned.
            if ($targetServerId === '' || (int) $targetServerId <= 0) {
                return false;
            }

            return $query->where('server', $targetServerId)->exists();
        }

        return false;
    }
}
