#!/usr/bin/env python3
"""Start the one-route MySQL-side weight test worker; never print credentials."""
import configparser
import hashlib
import os
from pathlib import Path

private_root = Path('/private/tmp/cdx-ai-weight-private-20261009.a72p8V')
source_root = Path('/private/tmp/zhct-http-ai-macos.x2CCZX')
local_env = Path('/Users/wiscourper/Documents/workspace/zhct/zhctproject/ai_api/.env.local')
php = '/opt/homebrew/Cellar/php@7.4/7.4.33_13/bin/php'
cfg = configparser.RawConfigParser()
cfg.optionxform = str
if not local_env.is_file():
    raise SystemExit('DENIED: code-local .env.local absent')
cfg.read_string('[DEFAULT]\n' + local_env.read_text())
ai = cfg['DATABASE']
store = cfg['DATABASE_ZHCT']
if (ai.get('TYPE', '').lower() != 'mysql' or ai.get('HOSTPORT') != '3306'
        or hashlib.sha256(ai.get('HOSTNAME', '').encode()).hexdigest()
            != '47e8be48b0cfd56dcdb9d9d426e6637b245f340b4efad4cd2f084ec5851ecc8e'
        or hashlib.sha256(ai.get('DATABASE', '').encode()).hexdigest()
            != 'cf7756b3bb52cf72f625689134015b877fdab3ad8eb3bb2ddd962bb35f6efd66'
        or store.get('TYPE', '').lower() != 'mysql'
        or store.get('HOSTPORT') != '3306'):
    raise SystemExit('DENIED: development MySQL identity drift')
if (not private_root.is_dir() or not (private_root / 'public/index.php').is_file()
        or not (private_root / 'external_stubs.php').is_file()
        or not (private_root / 'runtime/cache').is_dir()
        or (private_root / 'runtime/cache').resolve()
            == (source_root / 'runtime/cache').resolve()):
    raise SystemExit('DENIED: private worker/stub/cache boundary absent')
source = (source_root / 'app/api/service/user/UserWeight.php').resolve()
target = (private_root / 'app/api/service/user/UserWeight.php').resolve()
if source != target or not source.is_file():
    raise SystemExit('DENIED: compatibility source differs from code-local worker')
environment = os.environ.copy()
environment.update({
    'POC_DIALECT': 'mysql',
    'POC_EXTERNAL_STUBS': '1',
    'POC_TARGET_ROOT': str(private_root),
    'POC_MYSQL_UTF8MB4': '0',
    'POC_AI_HOST': ai['HOSTNAME'],
    'POC_AI_PORT': ai['HOSTPORT'],
    'POC_AI_SCHEMA': ai['DATABASE'],
    'POC_AI_USER': ai['USERNAME'],
    'POC_AI_PASSWORD': ai['PASSWORD'],
    'POC_HOST': store['HOSTNAME'],
    'POC_PORT': store['HOSTPORT'],
    'POC_SCHEMA': store['DATABASE'],
    'POC_USER': store['USERNAME'],
    'POC_PASSWORD': store['PASSWORD'],
})
os.execvpe(php, [php, '-S', '127.0.0.1:18211', '-t',
    str(private_root / 'public'), str(private_root / 'public/index.php')], environment)
