import psycopg2
from proteus import config

# CSV file paths
EMPLOYEE_CSV = 'loaders/employees.csv'
PARTIES_CSV = 'loaders/parties.csv'
CUSTOMER_STOCK_LOCATIONS_CSV = 'loaders/customer_stock_locations.csv'
SUPPLIER_STOCK_LOCATIONS_CSV = 'loaders/supplier_stock_locations.csv'
SERVICES_CSV = 'loaders/services.csv'
VESSELS_CSV = 'loaders/vessels.csv'

PURCHASE_CONTRACTS_CSV = 'Reference Data\python_project\loaders\Purchase_Contracts_with_mapping.csv'
PURCHASE_FEES_CSV = 'Reference Data\python_project\loaders\Purchase_Fees 2.csv'

SALE_CONTRACTS_CSV = 'Reference Data\python_project\loaders\Sale_Contracts_with_mapping.csv'
SALE_FEES_CSV = 'Reference Data\python_project\loaders\sale_fees.csv'
PRICES_CSV = 'Reference Data\python_project\loaders\Prices.csv'
PRICE_INDEXES_CSV = 'Reference Data\python_project\loaders\Price_Indexes.csv'


# XML-RPC Configuration (for Proteus interaction)
HTTPS = 'https://'
SERVER_URL = 'prod.open-squared.tech'
DATABASE_NAME = 'tradon'
USERNAME = 'admin'
PASSWORD = 'dsproject'

# PostgreSQL Configuration (for direct database access if needed)
DB_HOST = '62.72.36.116'   
DB_PORT = 5432                     
DB_USER = 'postgres'            
DB_PASSWORD = 'dsproject'


# Database connection configuration for direct PostgreSQL access
# Used by migration mapper and custom field updates
DB_CONFIG = {
    'host': DB_HOST,      # Your PostgreSQL host
    'port': DB_PORT,             # Your PostgreSQL port
    'database': DATABASE_NAME,  # Your Tryton database name
    'user': DB_USER,       # Your database user
    'password': DB_PASSWORD  # Your database password
}


# Function to connect to Tryton via XML-RPC
def connect_to_tryton():
    """Establish connection to Tryton via XML-RPC"""
    print(f"Server: {HTTPS}{SERVER_URL}")
    print(f"Database: {DATABASE_NAME}")
    print(f"Username: {USERNAME}")

    try:
        connection = config.set_xmlrpc(f'{HTTPS}{USERNAME}:{PASSWORD}@{SERVER_URL}/{DATABASE_NAME}/')
        print("✓ Connected successfully!\n")
        return connection
    except Exception as e:
        print(f"✗ Connection failed: {e}")
        print("\nTroubleshooting:")
        print("  - Verify the server URL is correct and accessible")
        print("  - Check that the Tryton server is running")
        print("  - Verify username and password are correct")
        print("  - Make sure you can access the server in a browser")
        raise
    


def get_db_connection():
    """Get PostgreSQL database connection"""
    try:
        connection = psycopg2.connect(
            host=DB_HOST,
            port=DB_PORT,
            database=DATABASE_NAME,
            user=DB_USER,
            password=DB_PASSWORD
        )
        return connection
    except Exception as e:
        print(f"✗ Database connection failed: {e}")
        raise