"""
介護保険計算のデバッグスクリプト
"""
from datetime import date
from app.core.database import get_connection

# 従業員20190001の情報を確認
employee_code = "20190001"
payment_date = date(2025, 12, 31)

print("=" * 60)
print(f"従業員コード: {employee_code}")
print(f"支給日: {payment_date}")
print("=" * 60)

with get_connection() as conn:
    with conn.cursor() as cur:
        # 従業員情報を取得
        cur.execute(
            """
            SELECT employee_id, employee_code, name, birth_date 
            FROM employees 
            WHERE employee_code = %s
            """,
            (employee_code,)
        )
        employee = cur.fetchone()
        
        if not employee:
            print("従業員が見つかりません")
            exit(1)
        
        print(f"\n【従業員情報】")
        print(f"  ID: {employee['employee_id']}")
        print(f"  コード: {employee['employee_code']}")
        print(f"  氏名: {employee['name']}")
        print(f"  生年月日: {employee['birth_date']}")
        
        # 年齢を計算
        if employee['birth_date']:
            birth_date = employee['birth_date']
            age = payment_date.year - birth_date.year
            if (payment_date.month, payment_date.day) < (birth_date.month, birth_date.day):
                age -= 1
            print(f"  年齢（{payment_date}時点）: {age}歳")
            print(f"  介護保険対象: {'はい (40-64歳)' if 40 <= age < 65 else 'いいえ'}")
        else:
            print(f"  生年月日が未設定です")
            age = 0
        
        # 介護保険料率を確認（2025年）
        print(f"\n【介護保険料率 - 2025年】")
        cur.execute(
            """
            SELECT * FROM insurance_rates 
            WHERE rate_type = '介護保険'
            AND rate_year = 2025
            ORDER BY rate_id
            """
        )
        rates_2025 = cur.fetchall()
        
        if rates_2025:
            for rate in rates_2025:
                print(f"  ID: {rate['rate_id']}")
                print(f"    計算対象: {rate['calculation_target']}")
                print(f"    都道府県: {rate['prefecture']}")
                print(f"    従業員負担率: {float(rate['employee_rate']) * 100:.3f}%")
                print(f"    会社負担率: {float(rate['employer_rate']) * 100:.3f}%")
                print(f"    年齢下限: {rate['care_insurance_age_threshold']}歳以上")
                print(f"    備考: {rate['notes']}")
                print()
        else:
            print("  ⚠️ 2025年の介護保険料率が見つかりません！")
        
        # 介護保険料率を確認（2026年）
        print(f"【介護保険料率 - 2026年】")
        cur.execute(
            """
            SELECT * FROM insurance_rates 
            WHERE rate_type = '介護保険'
            AND rate_year = 2026
            ORDER BY rate_id
            """
        )
        rates_2026 = cur.fetchall()
        
        if rates_2026:
            for rate in rates_2026:
                print(f"  ID: {rate['rate_id']}")
                print(f"    計算対象: {rate['calculation_target']}")
                print(f"    都道府県: {rate['prefecture']}")
                print(f"    従業員負担率: {float(rate['employee_rate']) * 100:.3f}%")
                print(f"    会社負担率: {float(rate['employer_rate']) * 100:.3f}%")
                print(f"    年齢下限: {rate['care_insurance_age_threshold']}歳以上")
                print()
        else:
            print("  2026年の介護保険料率が見つかりません")
        
        # 2025年12月の給与計算データを確認
        print(f"\n【2025年12月の給与計算】")
        cur.execute(
            """
            SELECT * FROM monthly_payroll 
            WHERE employee_id = %s
            AND payroll_year = 2025
            AND payroll_month = 12
            ORDER BY payroll_id DESC
            LIMIT 1
            """,
            (employee['employee_id'],)
        )
        payroll = cur.fetchone()
        
        if payroll:
            print(f"  Payroll ID: {payroll['payroll_id']}")
            print(f"  基本給: ¥{float(payroll['base_salary']):,.0f}")
            print(f"  健康保険: ¥{float(payroll['health_insurance']):,.0f}")
            print(f"  介護保険: ¥{float(payroll['care_insurance']):,.0f}")
            print(f"  厚生年金: ¥{float(payroll['pension_insurance']):,.0f}")
            print(f"  雇用保険: ¥{float(payroll['employment_insurance']):,.0f}")
            print(f"  計算日: {payroll['created_at']}")
        else:
            print("  給与計算データが見つかりません")

print("\n" + "=" * 60)
print("診断完了")
print("=" * 60)
