# thirdparty/trading212/views.py
import os
import base64
import logging
import requests
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated, AllowAny
from django.conf import settings
from django.core.cache import cache

from drf_spectacular.utils import extend_schema

logger = logging.getLogger(__name__)

api_key = os.getenv("ID_API_KEY_TRADING212")
api_secret = os.getenv("API_KEY_TRADING212")

base_url = "https://live.trading212.com"

class Trading212AccountCashView(APIView):
    permission_classes = [AllowAny]
    authentication_classes = []

    @extend_schema(
        tags=["trading212"],
        summary="Get Trading212 account cash"
    )
    def get(self, request):
        url = f"{base_url}/api/v0/equity/account/cash"

        response = requests.get(url, auth=(api_key, api_secret))

        logger.info("Trading212 Account Cash Response:")
        logger.info(response)

        data = response.json()

        return Response(data)
    
class Trading212SummaryView(APIView):
    permission_classes = [AllowAny]
    authentication_classes = []

    @extend_schema(
        tags=["trading212"],
        summary="Get Trading212 account summary"
    )
    def get(self, request):
        url = f"{base_url}/api/v0/equity/portfolio"
        response = requests.get(url, auth=(api_key, api_secret))

        logger.info("Trading212 Account Summary Response:")
        logger.info(response)

        data = response.json()

        return Response(data)