<?php
/*
 * AIWK Wallet MVP Installer
 * 설치 후 삭제 또는 접근 제한 권장.
 */
require_once dirname(__FILE__) . '/wallet_config.php';

header('Content-Type: text/html; charset=utf-8');

echo '<h2>AIWK Wallet MVP Installer</h2>';

$sql_file = dirname(__FILE__) . '/wallet_install.sql';
if (!file_exists($sql_file)) {
    echo '<p style="color:red">wallet_install.sql 파일이 없습니다.</p>';
    exit;
}

$sql = file_get_contents($sql_file);
$sql = str_replace("\r", "\n", $sql);
$sql = preg_replace('/\/\*.*?\*\//s', '', $sql);
$parts = explode(';', $sql);

$ok = 0;
$fail = 0;
foreach ($parts as $q) {
    $q = trim($q);
    if ($q === '') continue;
    try {
        sql_query($q, true, wallet_db());
        $ok++;
        echo '<div style="color:green">OK: ' . htmlspecialchars(substr($q, 0, 80), ENT_QUOTES, 'UTF-8') . '...</div>';
    } catch (Exception $e) {
        $fail++;
        echo '<div style="color:red">FAIL: ' . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8') . '</div>';
    }
}

echo '<hr>';
if ($fail === 0) {
    echo '<h3 style="color:green">' . wallet_t('install_ok') . '</h3>';
} else {
    echo '<h3 style="color:red">' . wallet_t('install_fail') . ' / OK=' . $ok . ' FAIL=' . $fail . '</h3>';
}

echo '<p>운영 전에는 wallet_install.php 파일을 삭제하거나 접근 제한하세요.</p>';
echo '<p><a href="wallet_demo.php">wallet_demo.php 열기</a></p>';
