import os
import sys

# 環境変数を設定
os.environ["DB_HOST"] = "192.168.0.61"
os.environ["DB_PORT"] = "55432"
os.environ["DB_NAME"] = "njts_acct"
os.environ["DB_USER"] = "njts_app"
os.environ["DB_PASSWORD"] = "njts_app2025"

sys.path.insert(0, r'c:\workspace\njts-accounting-core\backend')
from app.core.database import get_connection

try:
    with get_connection() as conn:
        with conn.cursor() as cur:
            print("standard_remunerationテーブルを作成中...")
            cur.execute("""
                CREATE TABLE IF NOT EXISTS standard_remuneration (
                    id SERIAL PRIMARY KEY,
                    rate_year INTEGER NOT NULL,
                    prefecture VARCHAR(50) NOT NULL,
                    grade VARCHAR(20) NOT NULL,
                    monthly_amount DECIMAL(12,2) NOT NULL,
                    salary_from DECIMAL(12,2) NOT NULL,
                    salary_to DECIMAL(12,2),
                    health_insurance_no_care DECIMAL(12,2) NOT NULL,
                    health_insurance_with_care DECIMAL(12,2) NOT NULL,
                    pension_insurance DECIMAL(12,2) NOT NULL,
                    UNIQUE(rate_year, prefecture, grade)
                )
            """)
            
            # インデックスを作成
            cur.execute("""
                CREATE INDEX IF NOT EXISTS idx_std_rem_year_pref 
                ON standard_remuneration(rate_year, prefecture)
            """)
            
            conn.commit()
            print("✓ テーブルとインデックスを作成しました")
            
            # テーブルの確認
            cur.execute("""
                SELECT column_name, data_type 
                FROM information_schema.columns 
                WHERE table_name = 'standard_remuneration'
                ORDER BY ordinal_position
            """)
            columns = cur.fetchall()
            print("\n作成されたカラム:")
            for col in columns:
                print(f"  - {col['column_name']}: {col['data_type']}")
                
except Exception as e:
    print(f"エラー: {e}")
    import traceback
    traceback.print_exc()
