"""NASにファイルを転送してコンテナを再起動するスクリプト"""
import paramiko
import os

HOST = "192.168.0.61"
USER = "root"
PASS = "59911784"
NAS_BASE = "/volume1/docker/njts-accounting"

files = [
    # (ローカルパス, NASパス)
    (r"c:\workspace\njts-accounting-core\backend\app\modules\bank_statements\__init__.py",
     f"{NAS_BASE}/backend/app/modules/bank_statements/__init__.py"),
    (r"c:\workspace\njts-accounting-core\backend\app\modules\bank_statements\router.py",
     f"{NAS_BASE}/backend/app/modules/bank_statements/router.py"),
    (r"c:\workspace\njts-accounting-core\backend\app\modules\bank_statements\migration.py",
     f"{NAS_BASE}/backend/app/modules/bank_statements/migration.py"),
    (r"c:\workspace\njts-accounting-core\backend\app\db_auto_migration.py",
     f"{NAS_BASE}/backend/app/db_auto_migration.py"),
    (r"c:\workspace\njts-accounting-core\backend\app\main.py",
     f"{NAS_BASE}/backend/app/main.py"),
    # e-Gov モジュール
    (r"c:\workspace\njts-accounting-core\backend\app\modules\egov\egov_api.py",
     f"{NAS_BASE}/backend/app/modules/egov/egov_api.py"),
    (r"c:\workspace\njts-accounting-core\backend\app\modules\egov\router.py",
     f"{NAS_BASE}/backend/app/modules/egov/router.py"),
    # フロントエンド
    (r"c:\workspace\njts-accounting-core\frontend\bank-statement.html",
     f"{NAS_BASE}/backend/frontend/bank-statement.html"),
    (r"c:\workspace\njts-accounting-core\frontend\index.html",
     f"{NAS_BASE}/backend/frontend/index.html"),
    (r"c:\workspace\njts-accounting-core\frontend\journal-entry.html",
     f"{NAS_BASE}/backend/frontend/journal-entry.html"),
    (r"c:\workspace\njts-accounting-core\frontend\trial-balance.html",
     f"{NAS_BASE}/backend/frontend/trial-balance.html"),
    (r"c:\workspace\njts-accounting-core\frontend\opening_balance.html",
     f"{NAS_BASE}/backend/frontend/opening_balance.html"),
    (r"c:\workspace\njts-accounting-core\frontend\egov.html",
     f"{NAS_BASE}/backend/frontend/egov.html"),
    (r"c:\workspace\njts-accounting-core\frontend\payroll.html",
     f"{NAS_BASE}/backend/frontend/payroll.html"),
]

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASS, timeout=30)
print(f"SSH接続: {HOST}")

# ディレクトリ作成
stdin, stdout, stderr = client.exec_command(
    f"mkdir -p {NAS_BASE}/backend/app/modules/bank_statements && mkdir -p {NAS_BASE}/backend/frontend"
)
stdout.read()
print("ディレクトリ作成完了")

# SFTPでファイル転送
sftp = client.open_sftp()
for local_path, remote_path in files:
    sftp.put(local_path, remote_path)
    print(f"転送: {os.path.basename(local_path)} → {remote_path}")
sftp.close()

# コンテナ再起動
print("\nコンテナ再起動中...")
cmd = f"cd {NAS_BASE} && docker compose restart backend"
stdin, stdout, stderr = client.exec_command(cmd, timeout=60)
out = stdout.read().decode()
err = stderr.read().decode()
code = stdout.channel.recv_exit_status()
if out: print("stdout:", out)
if err: print("stderr:", err)
print(f"再起動完了 (exit={code})")
client.close()
print("完了!")
