""" 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('/', methods=['PUT']) @token_required def update_customer(current_admin, customer_id): """Update customer information""" try: data = request.get_json() result = call_customer_api( f'/api/admin/customers/{customer_id}', method='PUT', data=data ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to update customer'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('/', methods=['DELETE']) @token_required def delete_customer(current_admin, customer_id): """Delete customer""" try: result = call_customer_api( f'/api/admin/customers/{customer_id}', method='DELETE' ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to delete customer'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('//suspend', methods=['POST']) @token_required def suspend_customer(current_admin, customer_id): """Suspend customer""" try: result = call_customer_api( f'/api/admin/customers/{customer_id}/suspend', method='POST' ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to suspend customer'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('//activate', methods=['POST']) @token_required def activate_customer(current_admin, customer_id): """Activate customer""" try: result = call_customer_api( f'/api/admin/customers/{customer_id}/activate', method='POST' ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to activate customer'}), 500 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 and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to update plan'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('//status', methods=['PUT']) @token_required def update_customer_status(current_admin, customer_id): """Update customer status""" try: data = request.get_json() result = call_customer_api( f'/api/admin/customers/{customer_id}/status', method='PUT', data=data ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to update status'}), 500 except Exception as e: return jsonify({'error': str(e)}), 500 @customers_bp.route('//password', methods=['PUT']) @token_required def change_customer_password(current_admin, customer_id): """Change customer password""" try: data = request.get_json() result = call_customer_api( f'/api/admin/customers/{customer_id}/password', method='PUT', data=data ) if result and result.get('status') == 'success': return jsonify(result), 200 else: return jsonify(result or {'error': 'Failed to change password'}), 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