""" Customer Management Routes (via Customer API) """ from flask import Blueprint, request, jsonify from app.routes.auth import token_required from app.config import Config import requests customers_bp = Blueprint('customers', __name__) def call_customer_api(endpoint, method='GET', data=None): """Helper to call customer platform API""" url = f"{Config.CUSTOMER_API_URL}{endpoint}" headers = { 'Content-Type': 'application/json', 'X-Internal-Key': Config.CUSTOMER_API_INTERNAL_KEY } try: if method == 'GET': response = requests.get(url, headers=headers, timeout=10) elif method == 'POST': response = requests.post(url, headers=headers, json=data, timeout=10) elif method == 'PUT': response = requests.put(url, headers=headers, json=data, timeout=10) elif method == 'DELETE': response = requests.delete(url, headers=headers, timeout=10) else: return None return response.json() if response.status_code < 500 else None except Exception as e: print(f"Error calling customer API: {e}") return None @customers_bp.route('', methods=['GET']) @token_required def get_customers(current_admin): """Get all customers from customer platform""" try: # TODO: Implement customer API endpoint result = call_customer_api('/api/admin/customers') if result: return jsonify(result), 200 else: return jsonify({ 'status': 'success', 'customers': [], 'message': 'Customer API not yet implemented' }), 200 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('/', methods=['GET']) @token_required def get_customer(current_admin, customer_id): """Get single customer""" try: result = call_customer_api(f'/api/admin/customers/{customer_id}') if result: return jsonify(result), 200 else: return jsonify({'error': 'Customer not found'}), 404 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('//plan', methods=['PUT']) @token_required def update_customer_plan(current_admin, customer_id): """Update customer's subscription plan""" try: data = request.get_json() result = call_customer_api( f'/api/admin/customers/{customer_id}/plan', method='PUT', data=data ) if result: return jsonify(result), 200 else: return jsonify({'error': 'Failed to update plan'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('/stats', methods=['GET']) @token_required def get_customer_stats(current_admin): """Get customer statistics""" try: result = call_customer_api('/api/admin/stats') if result: return jsonify(result), 200 else: # Return mock data for now return jsonify({ 'status': 'success', 'stats': { 'total_customers': 0, 'active_customers': 0, 'total_domains': 0, 'total_containers': 0 } }), 200 except Exception as e: return jsonify({'error': str(e)}), 500