import os
import mysql.connector
from flask import Blueprint, render_template

main = Blueprint('main', __name__)

@main.route('/config')
def config_page():
    try:
        conn = mysql.connector.connect(
            host=os.getenv('MYSQL_HOST'),
            user=os.getenv('MYSQL_USER'),
            password=os.getenv('MYSQL_PASSWORD'),
            database=os.getenv('MYSQL_DB'),
            port=int(os.getenv('MYSQL_PORT', 3306))
        )
        cur = conn.cursor()
        cur.execute("SELECT * FROM config LIMIT 1")
        row = cur.fetchone()
        config_data = {
            'company_name': row[0] if row else '',
            'logo_path': row[2] if row else '',
            'primary_color': row[3] if row else '#000000',
            'secondary_color': row[4] if row else '#FFFFFF',
        }
        cur.close()
        conn.close()
        return render_template("config.html", config=config_data)
    except Exception as e:
        return f"<h1>❌ ERROR: {str(e)}</h1>"
