#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
小説執筆支援システム統合CLI（本番版）
Novel Writing Support System Integrated CLI (Production)

本番モード：
- dist/ディレクトリの安定版コードを使用
- MCPサーバー専用エントリーポイント
- プロダクション最適化
"""

import os
import sys
import subprocess
from pathlib import Path

# Windows環境でのUTF-8出力を確保
if sys.platform == "win32":
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
    sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')

# 本番用フラグを設定
os.environ["NOVEL_PRODUCTION_MODE"] = "1"

# ガイドルートを特定
def find_guide_root():
    """ガイドルート（00_ガイド）を確実に特定"""
    current_file = Path(__file__).resolve()

    # bin/novelerからの相対パス
    if current_file.name == "noveler" and current_file.parent.name == "bin":
        return current_file.parent.parent

    # フォールバック
    return current_file.parent.parent

BASE_DIR = find_guide_root()

# パス定義
DIST_DIR = BASE_DIR / "dist"
DIST_MAIN = DIST_DIR / "noveler" / "main.py"
SRC_DIR = BASE_DIR / "src"
SRC_MAIN = SRC_DIR / "noveler" / "main.py"

# 本番モード表示
print("🏭 本番モードで実行中 - 安定版コードを使用しています")
print("")

# dist/パスの設定
selected_main = None
if DIST_MAIN.exists():
    path_sep = ";" if sys.platform == "win32" else ":"
    current_pythonpath = os.environ.get('PYTHONPATH', '')
    os.environ["PYTHONPATH"] = f"{str(DIST_DIR)}{path_sep}{current_pythonpath}" if current_pythonpath else str(DIST_DIR)
    os.environ["GUIDE_ROOT"] = str(BASE_DIR)
    os.environ["NOVEL_USE_DIST"] = "1"  # dist/ディレクトリ使用フラグ
    selected_main = DIST_MAIN
else:
    # Fallback: use source tree when dist build is not available
    fallback_path = str(SRC_DIR)
    path_sep = ";" if sys.platform == "win32" else ":"
    current_pythonpath = os.environ.get('PYTHONPATH', '')
    os.environ["PYTHONPATH"] = f"{fallback_path}{path_sep}{current_pythonpath}" if current_pythonpath else fallback_path
    os.environ["GUIDE_ROOT"] = str(BASE_DIR)
    os.environ.pop("NOVEL_USE_DIST", None)
    print("⚠️ distビルドが見つからなかったため、開発版ソース(src/)にフォールバックします")
    selected_main = SRC_MAIN

if not selected_main or not selected_main.exists():
    print(f"❌ 実行可能なmain.pyが見つかりません ({selected_main})")
    sys.exit(1)

# 本番用main.pyの実行
try:
    result = subprocess.run([sys.executable, str(selected_main)] + sys.argv[1:])
    sys.exit(result.returncode)
except Exception as e:
    print(f"❌ 本番用novelerコマンド実行エラー: {e}")
    sys.exit(1)
