#!/usr/bin/env python3

import os
import pathlib
import mysql.connector


def load_env_from_credentials(file_path: str = os.path.join(pathlib.Path(__file__).resolve().parents[2], '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()
    conn = mysql.connector.connect(
        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'),
    )
    try:
        cur = conn.cursor()
        tables = ['invoices','invoice_line_items','invoice_hs_codes','invoice_payments']
        for name in tables:
            cur.execute("SHOW TABLES LIKE %s", (name,))
            print(name, bool(cur.fetchone()))
    finally:
        conn.close()


if __name__ == '__main__':
    main()


