import psycopg2
import os
from dotenv import load_dotenv

load_dotenv()

DB_CONFIG = {
    "host": os.getenv("DB_HOST", "192.168.0.61"),
    "port": os.getenv("DB_PORT", "55432"),
    "database": os.getenv("DB_NAME", "njts_acct"),
    "user": os.getenv("DB_USER", "njts_app"),
    "password": os.getenv("DB_PASSWORD", "njts_app2025"),
}

def create_standard_remuneration_table():
    """標準報酬月額表テーブルを作成"""
    conn = psycopg2.connect(**DB_CONFIG)
    cur = conn.cursor()
    
    # テーブル作成
    sql = """
    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,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        UNIQUE(rate_year, prefecture, grade)
    );

    -- インデックス作成
    CREATE INDEX IF NOT EXISTS idx_standard_remuneration_year_pref 
        ON standard_remuneration(rate_year, prefecture);
    CREATE INDEX IF NOT EXISTS idx_standard_remuneration_salary 
        ON standard_remuneration(rate_year, prefecture, salary_from, salary_to);
    """
    
    cur.execute(sql)
    conn.commit()
    
    print("✅ standard_remuneration テーブルを作成しました")
    
    cur.close()
    conn.close()

if __name__ == "__main__":
    create_standard_remuneration_table()
