#!/usr/bin/env python3

import os
import mysql.connector


def load_env_from_credentials(file_path: str = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'db_credentials.txt')):
    creds = {}
    with open(file_path, 'r') as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith('#'):
                continue
            if '=' in line:
                key, val = line.split('=', 1)
                creds[key.strip()] = val.strip()
    return creds


def main():
    creds = load_env_from_credentials()
    host = creds.get('DB_HOST', 'localhost')
    port = int(creds.get('DB_PORT', '3306'))
    user = creds.get('DB_USER', 'root')
    password = creds.get('DB_PASSWORD', '')
    database = creds.get('DB_NAME', 'SouthernDirectionsql4')

    conn = mysql.connector.connect(host=host, port=port, user=user, password=password, database=database)
    try:
        cur = conn.cursor()
        cur.execute('SELECT DATABASE()')
        print('database:', cur.fetchone()[0])
        cur.execute("SHOW TABLES LIKE 'partners'")
        exists = cur.fetchone() is not None
        print('partners_table_exists:', exists)
        if exists:
            cur.execute('DESCRIBE partners')
            cols = cur.fetchall()
            print('columns:', [c[0] for c in cols])
            cur.execute('SELECT COUNT(*) FROM partners')
            print('row_count:', cur.fetchone()[0])
    finally:
        conn.close()


if __name__ == '__main__':
    main()



