<?php

declare(strict_types=1);

// Reads already-created isolated logs; never prints values, original bodies or user IDs.
$variant = (string)getenv('POC_VARIANT');
$files = [
    'B' => '/tmp/zhct-http-ai-macos.x2CCZX/runtime/store/log/20261002_info.log',
    'C' => '/tmp/zhct-ai-http-20260929.3XDj3w/runtime/store/log/20261002_info.log',
];
if (!isset($files[$variant]) || !is_file($files[$variant])) {
    throw new RuntimeException('Only existing B/C isolated Store log allowed');
}
$format = static function ($value): string {
    if (!is_string($value)) return gettype($value);
    if (preg_match('/^\d{4}-\d{2}-\d{2}$/D', $value)) return 'date';
    if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/D', $value)) {
        return 'datetime_seconds';
    }
    if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}$/D', $value)) {
        return 'datetime_micro6';
    }
    if (preg_match('/^\d{2}:\d{2}:\d{2}$/D', $value)) return 'time_seconds';
    if (preg_match('/^\d{2}:\d{2}:\d{2}\.\d{6}$/D', $value)) return 'time_micro6';
    return 'other_string';
};
$summary = [];
foreach (file($files[$variant]) as $line) {
    $entry = json_decode($line, true);
    if (!is_array($entry)) continue;
    $url = (string)($entry['url'] ?? '');
    foreach (['detail', 'dateRpe', 'detailRpe'] as $action) {
        if (!preg_match('/store\\.habit\\/' . preg_quote($action, '/') . '(?:\\?|$)/', $url)) {
            continue;
        }
        $response = json_decode((string)($entry['messages'] ?? ''), true);
        if (!is_array($response) || (int)($response['status'] ?? 0) !== 200) continue;
        $data = $response['data'] ?? null;
        if (!is_array($data)) continue;
        $row = ['data_keys' => array_keys($data), 'data_type' => gettype($data)];
        sort($row['data_keys']);
        if ($action === 'detail') {
            $items = $data['detail'] ?? null;
            $first = is_array($items) ? reset($items) : null;
            $row['detail_type'] = gettype($items);
            $row['detail_nonempty'] = is_array($items) && count($items) > 0;
            $row['detail_item_keys'] = is_array($first) ? array_keys($first) : [];
            sort($row['detail_item_keys']);
            $row['checked_type'] = is_array($first) ? gettype($first['checked'] ?? null) : 'missing';
        } elseif ($action === 'dateRpe') {
            $dates = $data['date_list'] ?? null;
            $row['date_list_type'] = gettype($dates);
            $row['date_item_type'] = is_array($dates) && isset($dates[0])
                ? gettype($dates[0]) : 'missing';
            $row['date_item_format'] = is_array($dates) && isset($dates[0])
                ? $format($dates[0]) : 'missing';
        } else {
            $detail = $data['detail'] ?? null;
            $info = is_array($detail) ? ($detail['info'] ?? null) : null;
            $first = is_array($info) ? reset($info) : null;
            $row['detail_type'] = gettype($detail);
            $row['detail_keys'] = is_array($detail) ? array_keys($detail) : [];
            sort($row['detail_keys']);
            $row['train_date_type'] = is_array($detail)
                ? gettype($detail['train_date'] ?? null) : 'missing';
            $row['train_date_format'] = is_array($detail)
                ? $format($detail['train_date'] ?? null) : 'missing';
            $row['info_type'] = gettype($info);
            $row['info_item_keys'] = is_array($first) ? array_keys($first) : [];
            sort($row['info_item_keys']);
            foreach (['start_time', 'end_time'] as $key) {
                $row[$key . '_type'] = is_array($first) ? gettype($first[$key] ?? null) : 'missing';
                $row[$key . '_format'] = is_array($first)
                    ? $format($first[$key] ?? null) : 'missing';
            }
        }
        $summary[$action] = $row;
    }
}
echo json_encode(['variant' => $variant, 'mode' => 'existing-log-key-type-only',
    'actions' => $summary], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
