import streamlit as st
import yfinance as yf
import alpaca_trade_api as alpaca
from newsapi import NewsApiClient
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from datetime import datetime, timedelta
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import logging
import threading
import time
import json
import os
import plotly.graph_objs as go
from sklearn.preprocessing import minmax_scale
from plotly.subplots import make_subplots
# Configure logging with timestamps
logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
)
logger = logging.getLogger(__name__)
# Use session state keys instead of file paths
AUTO_TRADE_LOG_KEY = "auto_trade_log"  # Session state key for trade log
AUTO_TRADE_INTERVAL = 10800  # Interval in seconds (e.g., 10800 seconds = 3 hours)
st.set_page_config(layout="wide")
class AlpacaTrader:
    def __init__(self, API_KEY, API_SECRET, BASE_URL):
        self.alpaca = alpaca.REST(API_KEY, API_SECRET, BASE_URL)
        self.cash = 0
        self.holdings = {}
        self.trades = []
    def get_market_status(self):
        return self.alpaca.get_clock().is_open
    def buy(self, symbol, qty, reason=None):
        try:
            # Ensure at least $1000 in cash before buying
            account = self.alpaca.get_account()
            cash_balance = float(account.cash)
            if cash_balance < 1000:
                logger.warning(f"Low cash: (${cash_balance}) to buy {symbol}. Minimum $1000 required.")
                return None
            order = self.alpaca.submit_order(symbol=symbol, qty=qty, side='buy', type='market', time_in_force='day')
            if reason:
                logger.info(f"Bought {qty} shares of {symbol} [Reason: {reason}]")
            else:
                logger.info(f"Bought {qty} shares of {symbol}")
            
            # Record the trade
            if order:
                self.trades.append({
                    'symbol': symbol, 
                    'qty': qty, 
                    'action': 'Buy', 
                    'time': datetime.now(),
                    'reason': reason
                })
            
            return order
        except Exception as e:
            logger.error(f"Error buying {symbol}: {e}")
            return None
    def sell(self, symbol, qty, reason=None):
        # Check if position exists and has enough quantity before attempting to sell
        positions = {p.symbol: float(p.qty) for p in self.alpaca.list_positions()}
        if symbol not in positions:
            logger.warning(f"No position in {symbol}. Sell not attempted.")
            return None
        if positions[symbol] < qty:
            logger.warning(f"Not enough shares to sell: {qty} requested, {positions[symbol]} available for {symbol}. Sell not attempted.")
            return None
        try:
            order = self.alpaca.submit_order(symbol=symbol, qty=qty, side='sell', type='market', time_in_force='day')
            if reason:
                logger.info(f"Sold {qty} shares of {symbol} [Reason: {reason}]")
            else:
                logger.info(f"Sold {qty} shares of {symbol}")
            
            # Record the trade
            if order:
                self.trades.append({
                    'symbol': symbol, 
                    'qty': qty, 
                    'action': 'Sell', 
                    'time': datetime.now(),
                    'reason': reason
                })
            
            return order
        except Exception as e:
            logger.error(f"Error selling {symbol}: {e}")
            return None
    def getHoldings(self):
        positions = self.alpaca.list_positions()
        for position in positions:
            self.holdings[position.symbol] = float(position.market_value)
        
        # Return holdings as a dictionary for internal use
        return self.holdings
    def getCash(self):
        return self.alpaca.get_account().cash
    def update_portfolio(self, symbol, price, qty, action):
        if action == 'buy':
            self.cash -= price * qty
            if symbol in self.holdings:
                self.holdings[symbol] += price * qty
            else:
                self.holdings[symbol] = price * qty
        elif action == 'sell':
            self.cash += price * qty
            self.holdings[symbol] -= price * qty
            if self.holdings[symbol] <= 0:
                del self.holdings[symbol]
        self.trades.append({'symbol': symbol, 'price': price, 'qty': qty, 'action': action, 'time': datetime.now()})
class NewsSentiment:
    def __init__(self, API_KEY):
        self.newsapi = NewsApiClient(api_key=API_KEY)
        self.sia = SentimentIntensityAnalyzer()
        self.alpha_vantage_api_key = st.secrets.get("ALPHA_VANTAGE_API_KEY")
        # Try to get Alpaca API for news fallback
        try:
            self.alpaca_api = alpaca.REST(
                st.secrets.get("ALPACA_API_KEY"),
                st.secrets.get("ALPACA_SECRET_KEY"),
                "https://paper-api.alpaca.markets"
            )
        except Exception as e:
            logger.error(f"Could not initialize Alpaca API for news fallback: {e}")
            self.alpaca_api = None
    def get_sentiment_and_headlines(self, symbol):
        """
        Try NewsAPI first, fallback to Alpha Vantage, then Alpaca news if needed.
        Returns (sentiment, headlines, source, avg_score).
        """
        # Try NewsAPI
        try:
            articles = self.newsapi.get_everything(q=symbol, language='en', sort_by='publishedAt', page=1)
            headlines = [a['title'] for a in articles.get('articles', [])[:5]]
            if headlines:
                sentiment, avg_score = self._calculate_sentiment(headlines, return_score=True)
                logger.info(f"NewsAPI sentiment for {symbol}: {sentiment} | Headlines: {headlines}")
                return sentiment, headlines, "NewsAPI", avg_score
            else:
                logger.warning(f"NewsAPI returned no headlines for {symbol}.")
        except Exception as e:
            logger.error(f"NewsAPI error for {symbol}: {e}")
            logger.info(f"Falling back to Alpha Vantage for {symbol} sentiment and headlines.")
        # Fallback to Alpha Vantage
        try:
            if self.alpha_vantage_api_key:
                import requests
                url = (
                    f"https://www.alphavantage.co/query?function=NEWS_SENTIMENT&tickers={symbol}"
                    f"&apikey={self.alpha_vantage_api_key}"
                )
                resp = requests.get(url)
                data = resp.json()
                headlines = [item.get("title") for item in data.get("feed", [])[:5] if item.get("title")]
                if headlines:
                    sentiment, avg_score = self._calculate_sentiment(headlines, return_score=True)
                    logger.info(f"Alpha Vantage sentiment for {symbol}: {sentiment} | Headlines: {headlines}")
                    return sentiment, headlines, "AlphaVantage", avg_score
                else:
                    logger.warning(f"Alpha Vantage returned no headlines for {symbol}.")
        except Exception as e:
            logger.error(f"Alpha Vantage error for {symbol}: {e}")
        # Fallback to Alpaca News API
        try:
            if self.alpaca_api:
                news_items = self.alpaca_api.get_news(symbol, limit=5)
                headlines = [item.headline for item in news_items if hasattr(item, "headline")]
                if headlines:
                    sentiment, avg_score = self._calculate_sentiment(headlines, return_score=True)
                    logger.info(f"Alpaca News sentiment for {symbol}: {sentiment} | Headlines: {headlines}")
                    return sentiment, headlines, "AlpacaNews", avg_score
                else:
                    logger.warning(f"Alpaca News returned no headlines for {symbol}.")
        except Exception as e:
            logger.error(f"Alpaca News error for {symbol}: {e}")
        logger.info(
            f"No sentiment/headlines available for {symbol} from NewsAPI, Alpha Vantage, or Alpaca News."
        )
        return None, [], None, None
    def _calculate_sentiment(self, headlines, return_score=False):
        if not headlines:
            return (None, None) if return_score else None
        compound_score = sum(self.sia.polarity_scores(title)['compound'] for title in headlines)
        avg_score = compound_score / len(headlines)
        if avg_score > 0.1:
            sentiment = 'Positive'
        elif avg_score < -0.1:
            sentiment = 'Negative'
        else:
            sentiment = 'Neutral'
        return (sentiment, avg_score) if return_score else sentiment
    def get_sentiment_bulk(self, symbols):
        """
        Bulk sentiment for a list of symbols using NewsAPI only (for auto-trade).
        Returns dict: symbol -> (sentiment, source).
        """
        sentiment = {}
        for symbol in symbols:
            try:
                articles = self.newsapi.get_everything(q=symbol, language='en', sort_by='publishedAt', page=1)
                headlines = [a['title'] for a in articles.get('articles', [])[:5]]
                if headlines:
                    s = self._calculate_sentiment(headlines)
                    logger.info(f"NewsAPI sentiment for {symbol}: {s} | Headlines: {headlines}")
                    sentiment[symbol] = (s, "NewsAPI")
                else:
                    # fallback to Alpha Vantage
                    s, _, src, _ = self.get_sentiment_and_headlines(symbol)
                    sentiment[symbol] = (s, src)
            except Exception as e:
                logger.error(f"Error getting news for {symbol}: {e}")
                # fallback to Alpha Vantage
                s, _, src, _ = self.get_sentiment_and_headlines(symbol)
                sentiment[symbol] = (s, src)
        return sentiment
class StockAnalyzer:
    def __init__(self, alpaca):
        self.alpaca = alpaca
        self.symbols = self.get_top_volume_stocks()
        # Build a symbol->name mapping for use in plots/tables
        self.symbol_to_name = self.get_symbol_to_name()
    def get_symbol_to_name(self):
        # Get mapping from symbol to company name using Alpaca asset info
        assets = self.alpaca.alpaca.list_assets(status='active')
        return {asset.symbol: asset.name for asset in assets}
    def get_bars(self, alp_api, symbols, timeframe='1D'):
        bars_data = {}
        try:
            bars = alp_api.get_bars(list(symbols), timeframe).df
            if 'symbol' not in bars.columns:
                logger.warning("The 'symbol' column is missing in the bars DataFrame.")
                return {symbol: {'bar_data': None} for symbol in symbols}
            for symbol in symbols:
                symbol_bars = bars[bars['symbol'] == symbol]
                if not symbol_bars.empty:
                    bar_info = symbol_bars.iloc[-1]
                    # Handle index type for timestamp
                    if isinstance(bar_info.name, tuple):
                        timestamp = bar_info.name[1].isoformat()
                    else:
                        timestamp = bar_info.name.isoformat()
                    bars_data[symbol] = {
                        'bar_data': {
                            'volume': bar_info['volume'],
                            'open': bar_info['open'],
                            'high': bar_info['high'],
                            'low': bar_info['low'],
                            'close': bar_info['close'],
                            'timestamp': timestamp
                        }
                    }
                else:
                    logger.debug(f"No bar data for symbol: {symbol}")
                    bars_data[symbol] = {'bar_data': None}
        except Exception as e:
            logger.warning(f"Error fetching bars in batch: {e}")
            for symbol in symbols:
                bars_data[symbol] = {'bar_data': None}
        return bars_data
    def assetswithconditions(self,stock_assets):
        cond = {
            'class': ['us_equity'],
            'exchange': ['NASDAQ', 'NYSE'],
            'status': ['active'],
            'tradable': [True],
            'marginable': [True],
            'shortable': [True],
            'easy_to_borrow': [True],
            'fractionable': [True]
        }
        assets_with_conditions = []
        asset_symbol_dict = {}
        
        for asset in stock_assets:
            # Skip symbols with '.' or '/' (preferred shares, warrants, etc.)
            if '.' in asset.symbol or '/' in asset.symbol:
                continue
            if (asset.__getattr__('class') in cond['class'] and 
                asset.exchange in cond['exchange'] and
                asset.status in cond['status'] and
                asset.tradable in cond['tradable'] and
                asset.marginable in cond['marginable'] and
                asset.shortable in cond['shortable'] and 
                asset.easy_to_borrow in cond['easy_to_borrow'] and
                asset.fractionable in cond['fractionable']
                ):
                assets_with_conditions.append(asset)
                
                asset_no_comma = asset.name.replace(',', '')
                asset_first_word = asset_no_comma.split()[0]
                asset_symbol_dict[asset.symbol] = asset._raw
                asset_symbol_dict[asset.symbol]['firstWord'] = asset_first_word
                
                sorted_dict = dict(sorted(asset_symbol_dict.items()))
        # print(f'Length of Alpaca assets with conditions = {len(assets_with_conditions)}')
        # print(f'assets_with_conditions = {assets_with_conditions}')
        return assets_with_conditions, sorted_dict
    def get_top_volume_stocks(self,num_stocks=10):
        try:
            # Get all tradable assets
            assets = self.alpaca.alpaca.list_assets(status='active')
            # tradable_assets = {asset.symbol: {} for asset in assets if asset.tradable}
            # print(f'tradable_assets = {tradable_assets}')
            assets_with_conditions, sorted_dict = self.assetswithconditions(assets)
            # print(f'sorted_dict = {sorted_dict}')
            # Fetch bar data for all tradable assets
            # print(f'sorted_dict.keys()={sorted_dict.keys()}')
            tradable_assets = self.get_bars(self.alpaca.alpaca, sorted_dict.keys(), timeframe='1D')
            # Extract volume and calculate the top 10 stocks by volume
            volume_data = {
                symbol: info['bar_data']['volume']
                for symbol, info in tradable_assets.items()
                if info['bar_data'] is not None
            }
            top_volume_stocks = sorted(volume_data, key=volume_data.get, reverse=True)[:num_stocks]
            logger.info(f'top_volume_stocks = {top_volume_stocks}')
            return top_volume_stocks
        except Exception as e:
            logger.error(f"Error fetching top volume stocks: {e}")
            return []
    def get_historical_data(self, symbols):
        data = {}
        for symbol in symbols:
            try:
                # Pull historical data from 2000-01-01 to today, daily interval
                ticker = yf.Ticker(symbol)
                hist = ticker.history(start='2023-01-01', end=datetime.now().strftime('%Y-%m-%d'), interval='1d')
                data[symbol] = hist
            except Exception as e:
                logger.error(f"Error getting data for {symbol}: {e}")
        return data
class TradingApp:
    def __init__(self):
        self.alpaca = AlpacaTrader(st.secrets['ALPACA_API_KEY'], st.secrets['ALPACA_SECRET_KEY'], 'https://paper-api.alpaca.markets')
        self.sentiment = NewsSentiment(st.secrets['NEWS_API_KEY'])
        self.analyzer = StockAnalyzer(self.alpaca)
        self.data = self.analyzer.get_historical_data(self.analyzer.symbols)
        self.auto_trade_log = []
    def display_charts(self):
        # Dynamically adjust columns based on number of stocks and available width
        symbols = list(self.data.keys())
        if not symbols:
            st.warning("No stock data available to display charts.")
            return  # Exit the function if no symbols are available
        symbol_to_name = self.analyzer.symbol_to_name
        n = len(symbols)
        # Calculate columns based on n for best fit
        cols = 3
        rows = (n + cols - 1) // cols
        subplot_titles = [
            f"{symbol} - {symbol_to_name.get(symbol, '')}" for symbol in symbols
        ]
        fig = make_subplots(rows=rows, cols=cols, subplot_titles=subplot_titles)
        for idx, symbol in enumerate(symbols):
            df = self.data[symbol]
            if not df.empty:
                row = idx // cols + 1
                col = idx % cols + 1
                fig.add_trace(
                    go.Scatter(
                        x=df.index,
                        y=df['Close'],
                        mode='lines',
                        name=symbol,
                        hovertemplate=f"%{{x}}
{symbol}: %{{y:.2f}}