import pandas as pd
import yfinance as yf
import plotly.graph_objs as go
from plotly import subplots
import plotly.io as pio
from datetime import datetime
pio.renderers.default = 'browser'

from config import *
from monitor_min import Monitor


class Simulation:

    def render_plotly(self, symbol: str, interval_minutes: int, data: pd.DataFrame, inverseData: pd.DataFrame) -> None:
        fig = subplots.make_subplots(
            rows=3, cols=1,
            subplot_titles=("캔들", "이격도/거래량", "장기 이격도"),
            shared_xaxes=True, horizontal_spacing=0.03, vertical_spacing=0.03,
            row_heights=[0.6, 0.2, 0.2]
        )

        # Row 1: 캔들 + 이동평균 + 볼린저
        fig.add_trace(go.Candlestick(x=data.index, open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], name='캔들'), row=1, col=1)
        for ma_col, color in [('MA5','red'),('MA20','blue'),('MA40','green'),('MA120','purple'),('MA200','brown'),('MA240','darkred'),('MA720','cyan'),('MA1440','magenta')]:
            if ma_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[ma_col], name=ma_col, mode='lines', line=dict(color=color, width=1)), row=1, col=1)
        if 'Lower' in data.columns and 'Upper' in data.columns:
            fig.add_trace(go.Scatter(x=data.index, y=data['Lower'], name='볼린저 하단', mode='lines', line=dict(color='grey', width=1, dash='dot')), row=1, col=1)
            fig.add_trace(go.Scatter(x=data.index, y=data['Upper'], name='볼린저 상단', mode='lines', line=dict(color='grey', width=1, dash='dot')), row=1, col=1)

        # 매수 포인트
        for sig, color in [('movingaverage','red'),('deviation40','orange'),('Deviation720','blue'),('deviation1440','purple'),('fall_6p','black')]:
            pts = data[(data['point']==1) & (data['signal']==sig)]
            if len(pts)>0:
                fig.add_trace(go.Scatter(x=pts.index, y=pts['Close'], mode='markers', name=f'{sig} 매수', marker=dict(color=color, size=8, symbol='circle')), row=1, col=1)

        # 매도 포인트: inverseData의 buy 신호 중 fall_6p, deviation40만 일반 그래프 가격축에 매도로 표시
        inv_sell_pts = inverseData[(inverseData['point']==1) & (inverseData['signal'].isin(['deviation40','fall_6p']))]
        if len(inv_sell_pts)>0:
            idx = inv_sell_pts.index.intersection(data.index)
            if len(idx)>0:
                fig.add_trace(
                    go.Scatter(
                        x=idx,
                        y=data.loc[idx, 'Close'],
                        mode='markers',
                        name='매도',
                        marker=dict(color='orange', size=10, symbol='triangle-down')
                    ),
                    row=1, col=1
                )

        # Row 2: 이격도 + 거래량
        for dev_col, color, width in [('Deviation5','red',1),('Deviation20','blue',1),('Deviation40','green',2),('Deviation120','purple',1),('Deviation200','brown',1),('Deviation720','darkred',2),('Deviation720','cyan',1),('Deviation1440','magenta',1)]:
            if dev_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[dev_col], name=dev_col, mode='lines', line=dict(color=color, width=width)), row=2, col=1)
        if 'Volume' in data.columns:
            fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='거래량', marker_color='lightgray', opacity=0.5), row=2, col=1)

        # Row 3: 장기 이격도 및 기준선
        for dev_col, color in [('Deviation720','darkred'),('Deviation1440','magenta')]:
            if dev_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[dev_col], name=f'{dev_col}(장기)', mode='lines', line=dict(color=color, width=2)), row=3, col=1)
        for h, color in [(90,'red'),(95,'green'),(100,'black')]:
            fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)

        # ----------------- 인버스용 트레이스 (초기 숨김) -----------------
        n_orig = len(fig.data)

        # Row 1: 캔들/MA/볼린저 (inverseData)
        fig.add_trace(go.Candlestick(x=inverseData.index, open=inverseData['Open'], high=inverseData['High'], low=inverseData['Low'], close=inverseData['Close'], name='캔들(인버스)', showlegend=True, visible=False), row=1, col=1)
        for ma_col, color in [('MA5','red'),('MA20','blue'),('MA40','green'),('MA120','purple'),('MA200','brown'),('MA240','darkred'),('MA720','cyan'),('MA1440','magenta')]:
            if ma_col in inverseData.columns:
                fig.add_trace(go.Scatter(x=inverseData.index, y=inverseData[ma_col], name=f'{ma_col}(인버스)', mode='lines', line=dict(color=color, width=1), showlegend=True, visible=False), row=1, col=1)
        if 'Lower' in inverseData.columns and 'Upper' in inverseData.columns:
            fig.add_trace(go.Scatter(x=inverseData.index, y=inverseData['Lower'], name='볼린저 하단(인버스)', mode='lines', line=dict(color='grey', width=1, dash='dot'), showlegend=True, visible=False), row=1, col=1)
            fig.add_trace(go.Scatter(x=inverseData.index, y=inverseData['Upper'], name='볼린저 상단(인버스)', mode='lines', line=dict(color='grey', width=1, dash='dot'), showlegend=True, visible=False), row=1, col=1)

        # 인버스 매수 포인트: fall_6p, deviation40만 표시
        for sig, color in [('deviation40','orange'),('fall_6p','black')]:
            pts_inv = inverseData[(inverseData['point']==1) & (inverseData['signal']==sig)]
            if len(pts_inv)>0:
                fig.add_trace(go.Scatter(x=pts_inv.index, y=inverseData.loc[pts_inv.index,'Close'], mode='markers', name=f'{sig} 매수(인버스)', marker=dict(color=color, size=8, symbol='circle'), showlegend=True, visible=False), row=1, col=1)

        # 인버스 보기에서의 매도 포인트: 일반 그래프의 매수를 인버스 그래프의 매도로 표시 (모든 매수 신호 반영)
        normal_to_inv_sell = data[(data['point']==1)]
        if len(normal_to_inv_sell) > 0:
            idx2 = normal_to_inv_sell.index.intersection(inverseData.index)
            if len(idx2) > 0:
                fig.add_trace(
                    go.Scatter(
                        x=idx2,
                        y=inverseData.loc[idx2, 'Close'],
                        mode='markers',
                        name='매도(일반→인버스)',
                        marker=dict(color='orange', size=10, symbol='triangle-down'),
                        showlegend=True,
                        visible=False
                    ),
                    row=1, col=1
                )

        # Row 2: 이격도 + 거래량 (inverseData)
        for dev_col, color, width in [('Deviation5','red',1),('Deviation20','blue',1),('Deviation40','green',2),('Deviation120','purple',1),('Deviation200','brown',1),('Deviation720','darkred',2),('Deviation720','cyan',1),('Deviation1440','magenta',1)]:
            if dev_col in inverseData.columns:
                fig.add_trace(go.Scatter(x=inverseData.index, y=inverseData[dev_col], name=f'{dev_col}(인버스)', mode='lines', line=dict(color=color, width=width), showlegend=True, visible=False), row=2, col=1)
        if 'Volume' in inverseData.columns:
            fig.add_trace(go.Bar(x=inverseData.index, y=inverseData['Volume'], name='거래량(인버스)', marker_color='lightgray', opacity=0.5, showlegend=True, visible=False), row=2, col=1)

        # Row 3: 장기 이격도 (inverseData)
        for dev_col, color in [('Deviation720','darkred'),('Deviation1440','magenta')]:
            if dev_col in inverseData.columns:
                fig.add_trace(go.Scatter(x=inverseData.index, y=inverseData[dev_col], name=f'{dev_col}(장기-인버스)', mode='lines', line=dict(color=color, width=2), showlegend=True, visible=False), row=3, col=1)

        n_total = len(fig.data)
        n_inv = n_total - n_orig
        visible_orig = [True]*n_orig + [False]*n_inv
        visible_inv = [False]*n_orig + [True]*n_inv
        legendtitle_orig = {'text': '일반 그래프'}
        legendtitle_inv = {'text': '인버스 그래프'}

        fig.update_layout(
            height=1000,
            margin=dict(t=180, l=40, r=240, b=40),
            title=dict(
                text=f"{symbol}, {interval_minutes} 분봉, ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})",
                x=0.5,
                xanchor='center',
                y=0.995,
                yanchor='top',
                pad=dict(t=10, b=12)
            ),
            xaxis_rangeslider_visible=False,
            xaxis1_rangeslider_visible=False,
            xaxis2_rangeslider_visible=False,
            legend=dict(orientation='v', yref='paper', yanchor='top', y=1.0, xref='paper', xanchor='left', x=1.02, title=legendtitle_orig),
            dragmode='zoom',
            updatemenus=[dict(
                type='buttons',
                direction='left',
                x=0.0,
                xanchor='left',
                y=1.11,
                yanchor='top',
                pad=dict(t=0, r=10, b=0, l=0),
                buttons=[
                    dict(
                        label='홈',
                        method='update',
                        args=[
                            {'visible': visible_orig},
                            {
                                'legend': {'title': legendtitle_orig},
                                'xaxis.autorange': True,
                                'xaxis2.autorange': True,
                                'xaxis3.autorange': True,
                                'yaxis.autorange': True,
                                'yaxis2.autorange': True,
                                'yaxis3.autorange': True,
                            }
                        ],
                        execute=True
                    ),
                    dict(
                        label='인버스',
                        method='update',
                        args=[
                            {'visible': visible_inv},
                            {'legend': {'title': legendtitle_inv, 'orientation': 'v', 'y': 1.0, 'yanchor': 'top', 'x': 1.02, 'xanchor': 'left'}}
                        ],
                        args2=[
                            {'visible': visible_orig},
                            {'legend': {'title': legendtitle_orig, 'orientation': 'v', 'y': 1.0, 'yanchor': 'top', 'x': 1.02, 'xanchor': 'left'}}
                        ],
                        execute=True
                    ),
                ]
            )]
        )
        fig.update_xaxes(title_text='시간', row=3, col=1)
        fig.update_yaxes(title_text='가격 (KRW)', row=1, col=1)
        fig.update_yaxes(title_text='이격도/거래량', row=2, col=1)
        fig.update_yaxes(title_text='장기 이격도', row=3, col=1)

        fig.show(config={'scrollZoom': True, 'displaylogo': False})
    def __init__(self) -> None:
        self.monitor = Monitor()
        self.INTERVAL_MAP = {
            60: "60m",
            240: "4h",
        }

    def detect_turnaround_signal(self, symbol, data, interval=0, params=None):
        if len(data) < 7:
            return None
        current_data = data.iloc[-1]
        if current_data.get('point', 0) == 1:
            return {
                'alert': True,
                'details': f"매수신호: {current_data.get('signal', 'unknown')}"
            }
        return {'alert': False, 'details': "매수신호 없음"}

    def fetch_price_history(self, symbol: str, interval_minutes: int, days: int = 30) -> pd.DataFrame:
        if symbol in KR_COINS:
            bong_count = 3000
            return self.monitor.get_coin_more_data(symbol, interval_minutes, bong_count=bong_count)
        if interval_minutes not in self.INTERVAL_MAP:
            raise ValueError("interval must be 60 or 240")
        interval_str = self.INTERVAL_MAP[interval_minutes]
        df = yf.download(
            tickers=symbol,
            period=f"{days}d",
            interval=interval_str,
            progress=False,
        )
        if df.empty:
            raise RuntimeError("No data fetched. Check symbol or interval support.")
        return df

    def analyze_bottom_period(self, symbol: str, interval_minutes: int, days: int = 90):
        data = self.fetch_price_history(symbol, interval_minutes, days)
        data = self.monitor.calculate_technical_indicators(data)
        data = self.monitor.annotate_signals(symbol, data, simulation=True)
        print(f"데이터 기간: {data.index[0]} ~ {data.index[-1]}")
        print(f"총 데이터 수: {len(data)}")
        bottom_start = pd.Timestamp('2025-06-22')
        bottom_end = pd.Timestamp('2025-07-09')
        bottom_data = data[(data.index >= bottom_start) & (data.index <= bottom_end)]
        if len(bottom_data) == 0:
            print("저점 기간 데이터가 없습니다.")
            return None, []
        print(f"\n저점 기간 데이터: {bottom_data.index[0]} ~ {bottom_data.index[-1]}")
        print(f"저점 기간 데이터 수: {len(bottom_data)}")
        print("\n=== 저점 기간 기술적 지표 분석 ===")
        min_price = bottom_data['Low'].min()
        max_price = bottom_data['High'].max()
        avg_price = bottom_data['Close'].mean()
        print(f"최저가: {min_price:.4f}")
        print(f"최고가: {max_price:.4f}")
        print(f"평균가: {avg_price:.4f}")
        print(f"가격 변동폭: {((max_price - min_price) / min_price * 100):.2f}%")
        bb_lower_min = bottom_data['Lower'].min()
        bb_upper_max = bottom_data['Upper'].max()
        print(f"\n볼린저 밴드 분석:")
        print(f"하단 밴드 최저: {bb_lower_min:.4f}")
        print(f"상단 밴드 최고: {bb_upper_max:.4f}")
        volume_avg = bottom_data['Volume'].mean()
        volume_max = bottom_data['Volume'].max()
        print(f"\n거래량 분석:")
        print(f"평균 거래량: {volume_avg:.0f}")
        print(f"최대 거래량: {volume_max:.0f}")
        actual_bottom_idx = bottom_data['Low'].idxmin()
        actual_bottom_price = bottom_data.loc[actual_bottom_idx, 'Low']
        actual_bottom_date = actual_bottom_idx
        print(f"\n실제 저점:")
        print(f"날짜: {actual_bottom_date}")
        print(f"가격: {actual_bottom_price:.4f}")
        print(f"볼린저 하단 대비: {((actual_bottom_price - bottom_data.loc[actual_bottom_idx, 'Lower']) / bottom_data.loc[actual_bottom_idx, 'Lower'] * 100):.2f}%")
        print(f"\n=== 매수 신호 분석 ===")
        bottom_alerts = bottom_data[bottom_data['point'] == 1]
        alerts = [(idx, row['Close']) for idx, row in bottom_alerts.iterrows()]
        print(f"저점 기간 매수 신호 수: {len(alerts)}")
        if alerts:
            print("매수 신호 발생 시점:")
            for date, price in alerts:
                print(f"  {date}: {price:.4f}")
        return bottom_data, alerts

    def run_simulation(self, symbol: str, interval_minutes: int, days: int = 30):
        data = self.fetch_price_history(symbol, interval_minutes)

        inverseData = self.monitor.inverse_data(data)
        inverseData = self.monitor.annotate_signals(symbol, inverseData, simulation=True)

        data = self.monitor.calculate_technical_indicators(data)
        data = self.monitor.annotate_signals(symbol, data, simulation=True)
        print(f"데이터 기간: {data.index[0]} ~ {data.index[-1]}")
        print(f"총 데이터 수: {len(data)}")
        alerts = []
        for i in range(len(data)):
            if data['point'].iloc[i] == 1:
                alerts.append((data.index[i], data['Close'].iloc[i]))
        print(f"\n총 매수 신호 수: {len(alerts)}")
        ma_signals = len(data[(data['point'] == 1) & (data['signal'] == 'movingaverage')])
        dev40_signals = len(data[(data['point'] == 1) & (data['signal'] == 'deviation40')])
        dev240_signals = len(data[(data['point'] == 1) & (data['signal'] == 'Deviation720')])
        dev1440_signals = len(data[(data['point'] == 1) & (data['signal'] == 'deviation1440')])
        print(f"  - MA 신호: {ma_signals}")
        print(f"  - Dev40 신호: {dev40_signals}")
        print(f"  - Dev240 신호: {dev240_signals}")
        print(f"  - Dev1440 신호: {dev1440_signals}")

        # Plotly 기반 시각화로 전환
        self.render_plotly(symbol, interval_minutes, data, inverseData)
        return

if __name__ == "__main__":
    sim = Simulation()
    interval = 60
    days = 90
    target_coins = ['XRP']
    show_graphs = True
    for symbol in target_coins:
        print(f"\n=== {symbol} 저점 기간 분석 시작 ===")
        try:
            bottom_data, alerts = sim.analyze_bottom_period(symbol, interval, days)
            print(f"\n=== {symbol} 전체 기간 시뮬레이션 ===")
            if show_graphs:
                sim.run_simulation(symbol, interval, days)
            else:
                data = sim.fetch_price_history(symbol, interval, days)

                inverseData = sim.monitor.inverse_data(data)
                inverseData = sim.monitor.annotate_signals(symbol, inverseData, simulation=True)

                data = sim.monitor.calculate_technical_indicators(data)
                data = sim.monitor.annotate_signals(symbol, data, simulation=True)

                total_signals = len(data[data['point'] == 1])
                ma_signals = len(data[(data['point'] == 1) & (data['signal'] == 'movingaverage')])
                dev40_signals = len(data[(data['point'] == 1) & (data['signal'] == 'deviation40')])
                dev240_signals = len(data[(data['point'] == 1) & (data['signal'] == 'Deviation720')])
                dev1440_signals = len(data[(data['point'] == 1) & (data['signal'] == 'deviation1440')])
                print(f"총 매수 신호: {total_signals}")
                print(f"  - MA 신호: {ma_signals}")
                print(f"  - Dev40 신호: {dev40_signals}")
                print(f"  - Dev240 신호: {dev240_signals}")
                print(f"  - Dev1440 신호: {dev1440_signals}")
        except Exception as e:
            print(f"Error analyzing {symbol}: {str(e)}")
