<?php
namespace PayCal\Licensing;

defined('ABSPATH') || exit;

class Gate {
  public static function has(string $module): bool {
    $data = LicenseStore::get();
    $license_key = (string) ($data['license_key'] ?? '');

    if (empty($license_key) || (class_exists(LicenseClient::class) && LicenseClient::looks_like_connector_credential($license_key))) return false;

    if (!empty($data['valid'])) {
      return in_array($module, (array)($data['modules'] ?? []), true);
    }

    if (LicenseStore::is_in_grace()) {
      return in_array($module, (array)($data['modules'] ?? []), true);
    }

    return false;
  }

  public static function status_label(): string {
    $data = LicenseStore::get();
    $license_key = (string) ($data['license_key'] ?? '');
    if (!empty($data['disabled'])) return 'Disabled';
    if (empty($license_key) || (class_exists(LicenseClient::class) && LicenseClient::looks_like_connector_credential($license_key))) return 'לא מחובר';
    if (!empty($data['valid'])) return 'Active';
    if (LicenseStore::is_in_grace()) return 'Grace period';
    if (empty($data['validated_at'])) return 'Pending verification';
    return 'Disabled';
  }
}
