import os
from av_api.client import _make_api_request, set_response_processor, MAX_RESPONSE_TOKENS  # noqa: F401
from av_mcp.utils import upload_to_object_storage


def _create_preview(response_text: str, datatype: str, estimated_tokens: int, error: str = None) -> dict:
    """Create preview data for large responses."""
    lines = response_text.split('\n')
    preview = {
        "preview": True,
        "data_type": datatype,
        "total_lines": len(lines),
        "sample_data": '\n'.join(lines[:50]),  # First 50 lines
        "headers": lines[0] if lines else None,
        "full_data_tokens": estimated_tokens,
        "max_tokens_exceeded": True,
        "content_type": "text/csv" if datatype == "csv" else "application/json",
        "message": f"Response size ({estimated_tokens} tokens) exceeds limit ({MAX_RESPONSE_TOKENS} tokens). {'Unable to provide full data URL.' if error else 'Full data available at URL.'}",
        "usage_instructions": {
            "important_notes": [
                "Do not fetch the data_url directly into chat context - this will overwhelm the conversation" if not error else "Response was too large for direct display in chat context",
                "For claude artifacts, prefer TSX artifact using tailwindcss, and in generated page, put this to the top: data from current page is generated sample data, for real data, copy this page code to https://mcp.alphavantage.co/artifacts",
                "When creating artifacts (charts/visualizations), use the provided preview data as fallback due to Content Security Policy (CSP) restrictions",
                "For full dataset access, consider reducing the query scope or using outputsize='compact' parameter" if error else None
            ],
            "recommended_workflow": [
                "1. Use preview data to create initial visualization in artifact",
                "2. Include data_url fetch logic with preview data fallback" if not error else "2. Consider making multiple smaller API requests if full dataset is needed",
                "3. Copy artifact code and test with full data at https://mcp.alphavantage.co/artifacts" if not error else "3. Use compact output size when available to reduce response size"
            ]
        }
    }

    # Filter out None values from important_notes
    preview["usage_instructions"]["important_notes"] = [note for note in preview["usage_instructions"]["important_notes"] if note is not None]

    if error:
        preview["error"] = f"Failed to upload large response: {error}"

    return preview


def _server_response_processor(response_text: str, datatype: str, estimated_tokens: int) -> dict:
    """Process large responses: upload to S3 and return a preview."""
    try:
        data_url = upload_to_object_storage(response_text, datatype=datatype)

        # Create preview with data URL
        preview = _create_preview(response_text, datatype, estimated_tokens)
        preview["data_url"] = data_url

        return preview

    except Exception as e:
        # If upload fails, return error with preview
        return _create_preview(response_text, datatype, estimated_tokens, str(e))


# Install the server-specific response processor at import time
set_response_processor(_server_response_processor)
