"""
Core views for the Dubai Analytics Platform.
"""
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import status
from django.http import JsonResponse
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator
from django.views.decorators.vary import vary_on_headers
from django.core.cache import cache
import logging

logger = logging.getLogger(__name__)


@api_view(['GET'])
@permission_classes([AllowAny])
def health_check(request):
    """
    Health check endpoint for monitoring.
    """
    return Response({
        'status': 'healthy',
        'service': 'Dubai Analytics API',
        'version': '1.0.0',
        'timestamp': cache.get('health_check_timestamp', 'unknown')
    })


@api_view(['GET'])
@permission_classes([AllowAny])
def api_info(request):
    """
    API information endpoint.
    """
    return Response({
        'name': 'Dubai Analytics API',
        'version': '1.0.0',
        'description': 'Multi-tenant SaaS platform for Dubai Land Department data analytics',
        'documentation': '/api/docs/',
        'endpoints': {
            'auth': '/api/v1/auth/',
            'analytics': '/api/v1/analytics/',
            'reports': '/api/v1/reports/',
            'integrations': '/api/v1/integrations/',
            'billing': '/api/v1/billing/',
            'monitoring': '/api/v1/monitoring/',
        }
    })


@api_view(['GET'])
def user_info(request):
    """
    Get current user information.
    """
    user = request.user
    return Response({
        'id': str(user.id),
        'email': user.email,
        'username': user.username,
        'subscription_type': user.subscription_type,
        'is_api_enabled': user.is_api_enabled,
        'company_name': user.company_name,
        'is_verified': user.is_verified,
        'date_joined': user.date_joined,
        'last_login': user.last_login,
    })


@api_view(['GET'])
def usage_stats(request):
    """
    Get user's API usage statistics.
    """
    user = request.user
    
    # Get usage limits
    limits = user.get_usage_limits()
    
    # Get current usage (this would need to be implemented with actual usage tracking)
    # For now, return placeholder data
    current_usage = {
        'api_calls_this_month': 0,
        'reports_this_month': 0,
        'forecast_requests_this_month': 0,
    }
    
    return Response({
        'subscription_type': user.subscription_type,
        'limits': limits,
        'current_usage': current_usage,
        'remaining': {
            'api_calls': limits.get('api_calls_per_month', 0) - current_usage['api_calls_this_month'],
            'reports': limits.get('reports_per_month', 0) - current_usage['reports_this_month'],
            'forecast_requests': limits.get('forecast_requests_per_month', 0) - current_usage['forecast_requests_this_month'],
        }
    })


def ratelimited_view(request):
    """
    View for rate limited requests.
    """
    return JsonResponse({
        'error': 'Rate limit exceeded',
        'message': 'Too many requests. Please try again later.',
        'retry_after': 60
    }, status=429)

