from microdot import Microdot, send_file
from settings import Settings
import json

controller = Microdot()
settings = Settings()

@controller.get('')
async def get_settings(request):
    """Get all settings."""
    # Settings is already a dict subclass; avoid dict() wrapper which can
    # trigger MicroPython's "dict update sequence has wrong length" quirk.
    return json.dumps(settings), 200, {'Content-Type': 'application/json'}

@controller.get('/wifi/ap')
async def get_ap_config(request):
    """Get saved AP configuration (Pi: no in-device AP)."""
    config = {
        'saved_ssid': settings.get('wifi_ap_ssid'),
        'saved_password': settings.get('wifi_ap_password'),
        'saved_channel': settings.get('wifi_ap_channel'),
        'active': False,
    }
    return json.dumps(config), 200, {'Content-Type': 'application/json'}

@controller.post('/wifi/ap')
async def configure_ap(request):
    """Save AP configuration to settings (Pi: no in-device AP)."""
    try:
        data = request.json
        ssid = data.get('ssid')
        password = data.get('password', '')
        channel = data.get('channel')
        
        if not ssid:
            return json.dumps({"error": "SSID is required"}), 400
        
        # Validate channel (1-11 for 2.4GHz)
        if channel is not None:
            channel = int(channel)
            if channel < 1 or channel > 11:
                return json.dumps({"error": "Channel must be between 1 and 11"}), 400
        
        settings['wifi_ap_ssid'] = ssid
        settings['wifi_ap_password'] = password
        if channel is not None:
            settings['wifi_ap_channel'] = channel
        settings.save()
        
        return json.dumps({
            "message": "AP settings saved",
            "ssid": ssid,
            "channel": channel
        }), 200, {'Content-Type': 'application/json'}
    except Exception as e:
        return json.dumps({"error": str(e)}), 500

@controller.put('/settings')
async def update_settings(request):
    """Update general settings."""
    try:
        data = request.json
        for key, value in data.items():
            settings[key] = value
        settings.save()
        return json.dumps({"message": "Settings updated successfully"}), 200, {'Content-Type': 'application/json'}
    except Exception as e:
        return json.dumps({"error": str(e)}), 500

@controller.get('/page')
async def settings_page(request):
    """Serve the settings page."""
    return send_file('templates/settings.html')

