<?php
declare(strict_types=1);

function ef_tools_index(?string $categorySlug=null):void
{
    $category=null;$where="c.type='tool' AND c.status='published' AND c.noindex=0";$params=[];if($categorySlug!==null){$category=one("SELECT * FROM categories WHERE slug=? AND type='tool_category'",[$categorySlug]);if(!$category){ef_error(404);return;}$where.=' AND c.category_id=?';$params[]=(int)$category['id'];}
    $rows=all("SELECT c.*,cat.name category_name,cat.slug category_slug FROM content c LEFT JOIN categories cat ON cat.id=c.category_id WHERE $where ORDER BY c.featured DESC,c.title",$params);$cats=all("SELECT cat.name,cat.slug,COUNT(c.id) total FROM categories cat JOIN content c ON c.category_id=cat.id AND c.type='tool' AND c.status='published' AND c.noindex=0 WHERE cat.type='tool_category' GROUP BY cat.id,cat.name,cat.slug ORDER BY cat.name");$title=$category?$category['name'].' Tools':'Free Student Tools';$description=$category?($category['description']?:'Free Exams.fit tools for '.$category['name'].'.'):'Fast, privacy-friendly calculators and study utilities for students in the United States.';
    $body='<section class="tools-hero"><p class="eyebrow">FREE • PRIVATE • NO SIGN-UP</p><h1>'.e($title).'</h1><p class="lead">'.e($description).'</p></section>';if($cats){$body.='<nav class="blog-categories" aria-label="Tool categories"><a '.($categorySlug===null?'aria-current="page"':'').' href="'.url('tools').'">All tools</a>';foreach($cats as$cat)$body.='<a '.($categorySlug===$cat['slug']?'aria-current="page"':'').' href="'.url('tools/category/'.$cat['slug']).'">'.e($cat['name']).' <span>'.(int)$cat['total'].'</span></a>';$body.='</nav>';}$body.='<section class="tool-grid">';foreach($rows as$r){$d=json_decode($r['data']??'{}',true)?:[];$body.='<article class="tool-card"><span class="tool-icon" aria-hidden="true">'.e($d['icon']??'⌁').'</span><p class="type">'.e($r['category_name']??'Student tool').'</p><h2><a href="'.url($r['slug']).'">'.e($r['title']).'</a></h2><p>'.e($r['excerpt']).'</p><a class="button" href="'.url($r['slug']).'">Open tool</a></article>';}$body.=($rows?'':'<p class="empty">Published tools will appear here after ZIP import and review.</p>').'</section>';
    $items=[];foreach($rows as$i=>$r)$items[]=['@type'=>'ListItem','position'=>$i+1,'name'=>$r['title'],'url'=>url($r['slug'])];$body.='<script type="application/ld+json">'.json_encode(['@context'=>'https://schema.org','@type'=>'CollectionPage','name'=>$title,'url'=>url($category?'tools/category/'.$category['slug']:'tools'),'mainEntity'=>['@type'=>'ItemList','numberOfItems'=>count($rows),'itemListElement'=>$items]],JSON_UNESCAPED_SLASHES|JSON_HEX_TAG).'</script>';ef_layout($body,['title'=>$title,'description'=>$description,'canonical'=>url($category?'tools/category/'.$category['slug']:'tools')]);
}

function ef_tool_widget(array $d):string
{
    $kind=$d['tool_type']??'';if(!in_array($kind,['grade_calculator','percentage_calculator','unit_converter','date_countdown','study_timer'],true))return'<div class="notice error">This imported tool configuration needs review.</div>';$fields=is_array($d['fields']??null)?array_slice($d['fields'],0,12):[];$safe=[];$h='<section class="tool-widget" data-ef-tool><h2>'.e($d['widget_title']??'Calculate').'</h2><form class="tool-form">';foreach($fields as$field){$name=preg_replace('/[^a-z0-9_]/','',strtolower((string)($field['name']??'')));$type=in_array($field['type']??'number',['number','date','select'],true)?$field['type']:'number';if($name==='')continue;$x=['name'=>$name,'type'=>$type];$h.='<label>'.e($field['label']??ucwords(str_replace('_',' ',$name)));if($type==='select'){$options=is_array($field['options']??null)?array_slice($field['options'],0,50):[];$x['options']=$options;$h.='<select name="'.e($name).'" required>';foreach($options as$o){$value=is_array($o)?(string)($o['value']??''):(string)$o;$label=is_array($o)?(string)($o['label']??$value):(string)$o;$h.='<option value="'.e($value).'">'.e($label).'</option>';}$h.='</select>';}else{$x['min']=$field['min']??null;$x['max']=$field['max']??null;$x['step']=$field['step']??($type==='number'?'any':null);$h.='<input type="'.$type.'" name="'.e($name).'" '.($x['min']!==null?'min="'.e((string)$x['min']).'" ':'').($x['max']!==null?'max="'.e((string)$x['max']).'" ':'').($x['step']!==null?'step="'.e((string)$x['step']).'" ':'').'required>';}$h.='</label>';$safe[]=$x;}$config=['tool_type'=>$kind,'fields'=>$safe,'units'=>$d['units']??[],'grade_scale'=>$d['grade_scale']??[],'result_label'=>$d['result_label']??'Result'];$encoded=base64_encode(json_encode($config,JSON_UNESCAPED_SLASHES));return$h.'<button class="button" type="submit">'.($kind==='study_timer'?'Start timer':'Calculate').'</button><button type="reset">Reset</button></form><output class="tool-result" aria-live="polite">Enter values to begin.</output><script type="application/json" data-tool-config>'.e($encoded).'</script></section><script defer src="'.url('assets/tools.js?v=3.1').'"></script>';
}
