<?php
// Public showcase reads the MDW Admin-managed projects file first.
// Falls back to the older /data/projects.json file if the admin file has not been created yet.
$adminProjectsFile = __DIR__ . '/../devworks-admin/data/projects.json';
$legacyProjectsFile = __DIR__ . '/../data/projects.json';
$projectsFile = file_exists($adminProjectsFile) ? $adminProjectsFile : $legacyProjectsFile;
$projects = file_exists($projectsFile) ? json_decode(file_get_contents($projectsFile), true) : [];
if (!is_array($projects)) $projects = [];
usort($projects, fn($a,$b) => ($a['sort'] ?? 100) <=> ($b['sort'] ?? 100));
function mdw_project_status_class($status) {
    $status = strtolower(trim((string)$status));
    if ($status === 'completed') return 'status-completed';
    if ($status === 'in progress') return 'status-progress';
    if ($status === 'planned') return 'status-planned';
    return 'status-default';
}
?>
<h2>Project Showcase</h2>
<p>Completed and in-progress work from Murdock DevWorks.</p>
<div class="project-showcase">
<?php foreach ($projects as $project): ?>
  <?php if (($project['status'] ?? '') === 'Hidden') continue; ?>
  <article class="project-card <?php echo mdw_project_status_class($project['status'] ?? ''); ?>">
    <span class="project-status"><?php echo htmlspecialchars($project['status'] ?? 'Project', ENT_QUOTES, 'UTF-8'); ?></span>
    <h3><?php echo htmlspecialchars($project['name'] ?? '', ENT_QUOTES, 'UTF-8'); ?></h3>
    <p><?php echo htmlspecialchars($project['description'] ?? '', ENT_QUOTES, 'UTF-8'); ?></p>
    <?php if (!empty($project['link']) && $project['link'] !== '#'): ?><a class="mdw-button ghost" href="<?php echo htmlspecialchars($project['link'], ENT_QUOTES, 'UTF-8'); ?>">Learn More</a><?php endif; ?>
  </article>
<?php endforeach; ?>
</div>
