"""
Update site contact phone number to 8368977491 in SQLite database.
All code contains proper English comments for clarity and maintenance.
"""

import sqlite3
import os

DB_PATH = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'instance', 'ngo.db')

def update_phone():
    if not os.path.exists(DB_PATH):
        print(f"Database not found at {DB_PATH}")
        return

    conn = sqlite3.connect(DB_PATH)
    cur = conn.cursor()

    new_phone = "+91 83689 77491"
    cur.execute("SELECT id FROM site_settings WHERE setting_key = 'contact.phone'")
    row = cur.fetchone()

    if row:
        cur.execute("UPDATE site_settings SET setting_value = ? WHERE setting_key = 'contact.phone'", (new_phone,))
        print(f"[OK] Updated existing contact.phone setting to '{new_phone}'")
    else:
        cur.execute("INSERT INTO site_settings (setting_key, setting_value) VALUES ('contact.phone', ?)", (new_phone,))
        print(f"[OK] Inserted new contact.phone setting '{new_phone}'")

    # Also update emergency contact default if present
    cur.execute("UPDATE users SET emergency_contact = ? WHERE emergency_contact = '+91 98765 43210'", (new_phone,))
    print(f"[OK] Updated emergency contact in users table if matching default")

    conn.commit()
    conn.close()
    print("[SUCCESS] Phone number updated successfully in database!")

if __name__ == '__main__':
    update_phone()
