<?php
///////////////////////////////////////////////////////////////////////////////
define('THIS_TOOL_NAME', 'index');
///////////////////////////////////////////////////////////////////////////////
include "includes/base-init.php";
include "includes/head-top.php";
///////////////////////////////////////////////////////////////////////////////

function findLoader(): string {
    $dir = __DIR__;
    for ($i = 0; $i < 6; $i++) {
        $candidate = $dir . '/lib/php/loader.php';
        if (file_exists($candidate)) {
            return $candidate;
        }
        $dir = dirname($dir);
    }
    die("Could not locate loader.php — ensure this tool is installed within the WMD SED directory tree.\n");
}
require findLoader();

$auth = new \CodePunch\Config\Auth(\CodePunch\DB\Database::STANDARD_INIT);
if (!$auth) {
    header("Location: ../index.php?logout&return=tools/wcag");
    exit;
}


function renderToolBox($file, $class, $title, $description) {
	global $auth;
	$hasProSupport = $auth->getProClass(pathinfo($file, PATHINFO_FILENAME), true);
	$requiresPro = $auth->hasProClass(pathinfo($file, PATHINFO_FILENAME), true);
	$toolPathFs   = __DIR__ . DIRECTORY_SEPARATOR . $file;
	$toolPathHref = "tools/" . $file;
	$exists       = file_exists($toolPathFs) && (!$requiresPro || $hasProSupport);

	$tag          = $exists ? 'a' : 'div';
	$hrefAttr     = $exists ? ' href="' . htmlspecialchars($toolPathHref, ENT_QUOTES, 'UTF-8') . '"' : '';
	$disabledCls  = $exists ? '' : ' tool-disabled';

	echo '<' . $tag . $hrefAttr . ' class="tool-box ' . htmlspecialchars($class, ENT_QUOTES, 'UTF-8') . $disabledCls . '">';
	echo '	<span class="tool-icon" aria-hidden="true"></span>';
	echo '	<div class="tool-content">';
	echo '		<h3>' . htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
	if ($requiresPro) {
		echo ' <span class="tool-pill">Requires Pro Edition</span>';
	}
	echo '		</h3>';
	echo '		<p>' . htmlspecialchars($description, ENT_QUOTES, 'UTF-8') . '</p>';
	echo '	</div>';
	echo '</' . $tag . '>';
}
?>
<body>
<div class="theme-select theme-bg" id="themechange"></div>
<div class="menu-select theme-bg" id="menuselect"></div>
<div id="contentwrap">
<?php include "includes/head-logo-toolbar.php"; ?>
<?php include "includes/login-required.php"; ?>
<div id="content">
	<div id="menucontrol">
<?php include "includes/index-sidebar.php"; ?>
	</div>
	<div id="maincontent">
		<div class="tools-panel">
			<h1>Custom Built-in Tools</h1>
			<p>Built-in tools for managing your domain portfolio. Import and export domains, view and filter your full portfolio, run system diagnostics, and check WCAG accessibility compliance. Available tools are determined by your administrator permissions.</p>
			<div class="tools-container">
				<?php
				renderToolBox(
					'add-domains.php',
					'tool-add-domains',
					'Add Domains',
					'Add individual domains or import them in bulk from a file. Administrators control which data fields are available during import, keeping entries consistent across the portfolio.'
				);

				renderToolBox(
					'export-domains.php',
					'tool-export-domains',
					'Export Domains',
					'Export your domain portfolio to CSV with full control over which columns to include, their order, and how they are labelled. A live preview lets you verify the output before downloading.'
				);

				renderToolBox(
					'portfolio.php',
					'tool-portfolio-domains',
					'Domain Portfolio',
					'A fully customisable view of your entire domain portfolio. Choose which columns to display, filter by category, trigger RDAP and WHOIS lookups, and manage category assignments from a single interface.'
				);

				renderToolBox(
					'diagnostics.php',
					'tool-diagnostics',
					'System Diagnostics',
					'Run a health check on your WMD SED installation. Covers system configuration, database connectivity, and network availability, with clear pass and fail results for each test.'
				);

				renderToolBox(
					'wcag.php',
					'tool-wcag',
					'WCAG Accessibility Report',
					'Scan domains in your portfolio for WCAG accessibility issues and review the results in one place. Violations are grouped by severity, with per-domain detail reports and export to CSV or JSON.'
				);

				renderToolBox(
					'lighthouse.php',
					'tool-lighthouse',
					'Lighthouse Report',
					'Scan domains in your portfolio using Google Lighthouse and review Performance, SEO, Accessibility, and Best Practices scores in one place. Results are shown for both mobile and desktop, with per-domain audit detail and export to CSV or JSON.'
				);
				?>
			</div>
		</div>
	</div>
</div>
</div>
<?php
///////////////////////////////////////////////////////////////////////////////
$jsApiBaseUrl = htmlspecialchars($apiBaseUrl, ENT_QUOTES, 'UTF-8');
///////////////////////////////////////////////////////////////////////////////
?>
<script>
var baseurl      = "<?php echo $baseUrl; ?>";
var API_BASE_URL = <?php echo json_encode($jsApiBaseUrl); ?>;
</script>
<script type="text/javascript" src="tools/js/common.js"></script>
<script type="text/javascript" src="tools/js/index.js"></script>
<script>
setTheme();
</script>
</body>
</html>