import sys
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:
            # テーブルの存在確認
            cur.execute("""
                SELECT EXISTS (
                    SELECT FROM information_schema.tables 
                    WHERE table_name = 'standard_remuneration'
                )
            """)
            exists = cur.fetchone()["exists"]
            print(f"テーブル存在: {exists}")
            
            if exists:
                # データ件数確認
                cur.execute("SELECT COUNT(*) as count FROM standard_remuneration")
                count = cur.fetchone()["count"]
                print(f"データ件数: {count}")
                
                # 年度リスト取得
                cur.execute("SELECT DISTINCT rate_year FROM standard_remuneration ORDER BY rate_year DESC")
                years = cur.fetchall()
                print(f"年度リスト: {years}")
            else:
                print("テーブルが存在しません。作成します...")
                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)
                    )
                """)
                conn.commit()
                print("テーブルを作成しました")
                
except Exception as e:
    print(f"エラー: {e}")
    import traceback
    traceback.print_exc()
