# app/routes/admin.py
import datetime

from flask import Blueprint, render_template, request, jsonify, redirect, url_for
from app.state import current_config
from app.utils.helpers import load_configs, load_default_sounds

admin_bp = Blueprint('admin', __name__)

@admin_bp.route('/')
def admin():
    configs = load_configs()
    return render_template('admin.html', configs=configs)


@admin_bp.route('/edit/<filename>', methods=['GET', 'POST'])
def admin_edit_config(filename):
    path = os.path.join(app.config['CONFIG_DIR'], filename)
    
    if request.method == 'POST':
        try:
            new_content = request.form.get('config_content')
            if not new_content:
                raise ValueError("Kein Inhalt übermittelt")
            
            # Validierung: versuche zu parsen
            json.loads(new_content)
            
            with open(path, 'w', encoding='utf-8') as f:
                f.write(new_content)
            
            logger.info(f"Config {filename} erfolgreich gespeichert")
            return redirect(url_for('admin'))
        
        except json.JSONDecodeError as e:
            logger.error(f"Ungültiges JSON in {filename}: {e}")
            error_msg = f"Ungültiges JSON: {str(e)}"
        except Exception as e:
            logger.error(f"Speicherfehler {filename}: {e}")
            error_msg = f"Fehler beim Speichern: {str(e)}"
        
        # Bei Fehler: zurück zum Formular mit Fehlermeldung
        with open(path, 'r', encoding='utf-8') as f:
            content = f.read()
        return render_template('admin_edit.html', filename=filename, content=content, error=error_msg)
    
    # GET: Formular laden
    if not os.path.exists(path):
        return "Konfiguration nicht gefunden", 404
    
    with open(path, 'r', encoding='utf-8') as f:
        content = f.read()
    
    return render_template('admin_edit.html', filename=filename, content=content)
    pass

@admin_bp.route('/delete/<filename>', methods=['POST'])
def admin_delete_config(filename):
    path = os.path.join(app.config['CONFIG_DIR'], filename)
    if os.path.exists(path):
        try:
            os.remove(path)
            logger.info(f"Config gelöscht: {filename}")
            return jsonify({"success": True, "message": f"{filename} gelöscht"})
        except Exception as e:
            logger.error(f"Löschfehler {filename}: {e}")
            return jsonify({"success": False, "message": str(e)}), 500
    return jsonify({"success": False, "message": "Datei nicht gefunden"}), 404
    pass

@admin_bp.route('/logs')
@admin_bp.route('/logs/<date>')
def admin_logs(date=None):
    if date is None:
        date = datetime.now().strftime('%d')
    
    today = datetime.now().strftime('%Y-%m')  # ← Hier definieren!
    current_month = datetime.now().strftime('%Y-%m')

    log_dir = os.path.join(LOG_DIR, today)
    
    if not os.path.exists(log_dir):
        return render_template('admin_logs.html', logs="Keine Logs für diesen Tag.", date=date, dates=[], today=today)

    # Verfügbare Tage ...
    dates = sorted([
        f.split('-')[0] for f in os.listdir(log_dir)
        if f.endswith('-info.log') or f.endswith('-error.log')
    ], reverse=True)

    # Logs laden ...
    info_path = os.path.join(log_dir, f"{date}-info.log")
    error_path = os.path.join(log_dir, f"{date}-error.log")

    logs = []
    if os.path.exists(info_path):
        with open(info_path, 'r', encoding='utf-8') as f:
            logs.append("=== INFO-LOG ===\n" + f.read())
    if os.path.exists(error_path):
        with open(error_path, 'r', encoding='utf-8') as f:
            logs.append("=== ERROR-LOG ===\n" + f.read())

    log_content = "\n\n".join(logs) if logs else "Keine Logs für diesen Tag."

    return render_template('admin_logs.html', 
                           logs=log_content, 
                           date=date, 
                           dates=dates,
                           today=current_month)
    pass