<?php
function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
function clean_id($id){ return preg_replace('/[^a-zA-Z0-9_\-]/','', (string)$id); }
function read_json($file,$fallback){ if(!file_exists($file)) return $fallback; $j=json_decode(file_get_contents($file),true); return is_array($j)?$j:$fallback; }
$id = clean_id($_GET['id'] ?? '');
$savesDir = __DIR__ . '/data/saves';
$indexFile = __DIR__ . '/data/share-index.json';
$file = $id ? $savesDir . '/' . $id . '.json' : '';
$data = ($id && file_exists($file)) ? json_decode(file_get_contents($file), true) : null;
if(!is_array($data)) $data = null;
$https = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (($_SERVER['SERVER_PORT'] ?? '') == 443);
$scheme = $https ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$basePath = rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? ''), '/\\');
$baseUrl = $scheme . '://' . $host . ($basePath ? $basePath : '');
$canonical = $baseUrl . '/' . ($id ? 'share.php?id=' . rawurlencode($id) : 'share.php');
$title = $data ? (($data['outcomeLabel'] ?? 'Case Archive') . ': ' . ($data['title'] ?? 'Step Away Case')) : 'Step Away Shared Case Archive';
$desc = $data ? (($data['story']['summary'] ?? $data['opening'] ?? '') ?: 'A shared Step Away case story.') : 'Read shared Step Away case stories and open the game.';
if(strlen($desc) > 200) $desc = substr($desc, 0, 197) . '...';
$ogImage = $baseUrl . '/icons/icon-512.png';
function card_names($play){ $names=[]; foreach(($play['cards'] ?? []) as $c){ $names[]=$c['title'] ?? $c['id'] ?? 'Card'; } return implode(' + ', $names); }
function teaching_points($data){ return $data['teachingPoints'] ?? $data['story']['teaching'] ?? ['Notice the shift.','Stay with the person.','Make help real.','Protect the hour.']; }
function outcome_class($data){ $o=$data['outcome'] ?? 'partial'; return in_array($o,['saved','partial','lost'],true)?$o:'partial'; }
?><!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="theme-color" content="#090b0e"><title><?=h($title)?></title><meta name="description" content="<?=h($desc)?>"><meta property="og:title" content="<?=h($title)?>"><meta property="og:description" content="<?=h($desc)?>"><meta property="og:type" content="article"><meta property="og:url" content="<?=h($canonical)?>"><meta property="og:image" content="<?=h($ogImage)?>"><link rel="icon" href="icons/icon-192.png"><link rel="stylesheet" href="assets/css/app.css?v=23"></head>
<body class="share-body narrative-share"><main class="share-page">
<?php if(!$data && !$id): $idx = read_json($indexFile, ['shares'=>[]]); $shares = array_reverse($idx['shares'] ?? [], true); $shares = array_slice($shares, 0, 24, true); ?>
<section class="share-hero panel"><p class="eyebrow">Step Away Archive</p><h1>Shared case stories</h1><p>Each link is a public case archive from a table play. You do not need to know the rules first: read the story, see the cards that shaped it, then open the app to play your own case.</p><div class="share-actions"><a class="primary" href="index.php">Open the case console</a><a class="secondary" href="official-deck.html">Official deck</a></div></section>
<section class="panel"><h2>Recent public cases</h2><?php if(!$shares): ?><p>No public case stories have been shared yet. Finish a case in the app and publish the outcome to seed this page.</p><?php else: ?><div class="share-list"><?php foreach($shares as $sid=>$s): ?><a class="quick-card quick-link" href="share.php?id=<?=h($sid)?>"><b><?=h($s['title'] ?? 'Shared case')?></b><span><?=h(ucfirst($s['outcome'] ?? 'partial'))?> · <?=h($s['createdAt'] ?? '')?></span></a><?php endforeach; ?></div><?php endif; ?></section>
<?php elseif(!$data): ?>
<section class="share-hero panel"><p class="eyebrow">Step Away Archive</p><h1>Shared case not found</h1><p>This public case link may have expired, been removed, or not reached this server.</p><div class="share-actions"><a class="primary" href="index.php">Open Step Away</a><a class="secondary" href="share.php">Shared cases</a></div></section>
<?php else: ?>
<section class="share-hero panel"><p class="eyebrow"><?=h($data['categoryTitle'] ?? 'Step Away shared case')?></p><h1><?=h($data['title'] ?? 'Shared case')?></h1><p class="share-subtitle"><?=h($data['subtitle'] ?? '')?></p><div class="share-result <?=h(outcome_class($data))?>"><?=h($data['outcomeLabel'] ?? 'Outcome')?></div><div class="share-stats"><span><?=h(($data['players'] ?? 1) . ' player' . (($data['players'] ?? 1)==1?'':'s'))?></span><span><?=h(($data['deckMode'] ?? 'official') . ' deck')?></span><span><?=h('Progress ' . (($data['progress']['value'] ?? 0)) . '/' . (($data['progress']['target'] ?? 0)))?></span><span><?=h('Pressure ' . (($data['pressure']['value'] ?? 0)) . '/' . (($data['pressure']['limit'] ?? 0)))?></span><span><?=h(count($data['plays'] ?? []))?> cards/events</span></div><p><?=h($data['opening'] ?? '')?></p><?php if(!empty($data['caseVoice'])): ?><p><?=h($data['caseVoice'])?></p><?php endif; ?><div class="share-actions"><a class="primary" href="index.php">Play Step Away</a><a class="secondary" href="official-deck.html">Official deck</a><a class="secondary" href="share.php">More shared cases</a></div></section>
<?php if(!empty($data['story']['sections'])): foreach($data['story']['sections'] as $sec): ?><article class="share-moment panel novella-section"><p class="eyebrow"><?=h($sec['kicker'] ?? 'Story')?></p><h2><?=h($sec['title'] ?? '')?></h2><?php foreach(($sec['paragraphs'] ?? []) as $para): ?><p><?=h($para)?></p><?php endforeach; ?></article><?php endforeach; else: foreach(($data['moments'] ?? []) as $m): ?><article class="share-moment panel novella-section"><p class="eyebrow">Moment <?=h($m['index'] ?? '')?></p><h2><?=h($m['title'] ?? 'Moment')?></h2><p><?=h($m['text'] ?? '')?></p></article><?php endforeach; endif; ?>
<section class="panel played-archive"><p class="eyebrow">Cards that shaped the story</p><h2>Intervention trail</h2><div class="share-card-row"><?php foreach(($data['plays'] ?? []) as $p): ?><article class="share-play <?=!empty($p['crisis'])?'crisis':''?>"><div class="share-card-images"><?php foreach(($p['cards'] ?? []) as $c): if(!empty($c['front'])): ?><img src="<?=h($c['front'])?>" alt="<?=h($c['title'] ?? 'Card')?>"><?php endif; endforeach; ?></div><h3><?=h(card_names($p))?></h3><p><b><?=h(($p['kind'] ?? 'Intervention') . ' · ' . ($p['result'] ?? '') . ' · VALUE ' . ($p['value'] ?? ''))?></b><?=!empty($p['target']) ? h(' / target ' . $p['target']) : ''?></p><?php if(!empty($p['story']['body'])): ?><p><?=h($p['story']['body'])?></p><?php elseif(!empty($p['note'])): ?><p><?=h($p['note'])?></p><?php endif; ?><?php if(!empty($p['story']['lesson'])): ?><p class="share-note"><?=h($p['story']['lesson'])?></p><?php endif; ?></article><?php endforeach; ?></div></section>
<section class="panel"><p class="eyebrow">What this case teaches</p><h2>Awareness notes</h2><ul><?php foreach(teaching_points($data) as $pt): ?><li><?=h($pt)?></li><?php endforeach; ?></ul><p class="muted">Step Away is a game and awareness rehearsal tool. It is not a substitute for emergency help, clinical support or crisis services.</p></section>
<section class="panel"><p class="eyebrow">Final archive</p><h2><?=h($data['outcomeLabel'] ?? 'Outcome')?></h2><p><?=h($data['closing'] ?? '')?></p><?php if(!empty($data['plays'])): ?><div class="story-card-strip"><?php foreach($data['plays'] as $p): foreach(($p['cards'] ?? []) as $c): if(!empty($c['front'])): ?><img src="<?=h($c['front'])?>" alt="<?=h($c['title'] ?? 'Card')?>"><?php endif; endforeach; endforeach; ?></div><?php endif; ?><div class="share-actions"><a class="primary" href="index.php">Open the app</a><a class="secondary" href="official-deck.html">Get the official deck</a></div></section>
<?php endif; ?></main></body></html>
