"""
=========================================
CV6 AI Trading OS
Angel One SmartAPI Service
=========================================
"""

from SmartApi import SmartConnect
import pyotp

from app.core.credentials import (
    ANGEL_API_KEY,
    ANGEL_CLIENT_ID,
    ANGEL_PASSWORD,
    ANGEL_TOTP_SECRET,
)


class AngelService:

    def __init__(self):
        self.client = None
        self.session = None

    def login(self):
        """
        Login to Angel One SmartAPI
        """

        if self.client is None:
            self.client = SmartConnect(
                api_key=ANGEL_API_KEY
            )

        self.session = self.client.generateSession(
            ANGEL_CLIENT_ID,
            ANGEL_PASSWORD,
            pyotp.TOTP(
                ANGEL_TOTP_SECRET
            ).now()
        )

        return self.session

    def logout(self):
        """
        Logout
        """
        return {
            "success": True
        }

    def profile(self):
        """
        Get Profile
        """
        return {}

    def funds(self):
        """
        Get Funds
        """
        return {}

    def holdings(self):
        """
        Get Holdings
        """
        return []

    def positions(self):
        """
        Get Positions
        """
        return []

    def orders(self):
        """
        Get Orders
        """
        return []

    def place_order(self, order):
        """
        Place Order
        """
        return {
            "success": True,
            "order_id": "TEST123"
        }

    def modify_order(self, order_id, order):
        """
        Modify Order
        """
        return {
            "success": True
        }

    def cancel_order(self, order_id):
        """
        Cancel Order
        """
        return {
            "success": True
        }

    def quotes(self, symbol):
        """
        Live Quote
        """
        return {}

    def historical_data(self, symbol):
        """
        Historical Data
        """
        return []

    def live_data(self, symbol):
        """
        Live Tick Data
        """
        return {}