from .base_strategy import BaseStrategy
from fetchers.factory import FetcherFactory
from analysis.jp_analyzer import JP_Analyzer
from reporting.jp_report_generator import JP_ReportGenerator
from storage.file_io import DataStorage
import os

class JP_Strategy(BaseStrategy):
    def __init__(self, stock_code, ifind_refresh_token):
        super().__init__(stock_code)
        self.refresh_token = ifind_refresh_token
        self.fetcher = FetcherFactory.get_fetcher('JP', ifind_refresh_token=self.refresh_token)
        self.analyzer = JP_Analyzer()
        self.reporter = JP_ReportGenerator()
        self.storage = DataStorage()
        self.raw_data = {}
        self.analysis_result = None

    def fetch_data(self):
        print(f"Fetching data for JP market, stock: {self.stock_code}")
        # Fetch Financial Statements
        self.raw_data['income'] = self.fetcher.get_income_statement(self.stock_code)
        self.raw_data['balance'] = self.fetcher.get_balance_sheet(self.stock_code)
        self.raw_data['cashflow'] = self.fetcher.get_cash_flow(self.stock_code)
        
        # Rename 'end_date' to 'date' for analyzer compatibility (BaseAnalyzer expects 'date' to create 'date_str')
        for key in ['income', 'balance', 'cashflow']:
            if not self.raw_data[key].empty and 'end_date' in self.raw_data[key].columns:
                self.raw_data[key] = self.raw_data[key].rename(columns={'end_date': 'date'})
        
        # Fetch Market Metrics (Real-time and static)
        self.raw_data['metrics'] = self.fetcher.get_market_metrics(self.stock_code)
        
        # Fetch Historical Metrics (Price and Market Cap)
        # We'll use some representative dates or the last 3 years of month-ends if needed.
        # For consistency with CN, let's grab the dates from the income statement or simple defaults.
        dates = []
        if not self.raw_data['income'].empty and 'date' in self.raw_data['income'].columns:
            dates = self.raw_data['income']['date'].tolist()
        
        self.raw_data['historical_metrics'] = self.fetcher.get_historical_metrics(self.stock_code, dates)
        
        # Fetch Dividends
        self.raw_data['dividends'] = self.fetcher.get_dividends(self.stock_code)
        
        # Fetch Repurchases
        self.raw_data['repurchases'] = self.fetcher.get_repurchases(self.stock_code)
        
        # Fetch Employee Count
        self.raw_data['employee_count'] = self.fetcher.get_employee_count(self.stock_code)

    def analyze_data(self):
        print(f"Analyzing data for JP market, stock: {self.stock_code}")
        self.analysis_result = self.analyzer.process_data(
            self.raw_data['income'],
            self.raw_data['balance'],
            self.raw_data['cashflow'],
            self.raw_data['metrics'],
            self.raw_data.get('historical_metrics'),
            self.raw_data.get('dividends'),
            self.raw_data.get('repurchases'),
            self.raw_data.get('employee_count')
        )

    def generate_report(self):
        print(f"Generating report for JP market, stock: {self.stock_code}")
        if self.analysis_result is not None and not self.analysis_result.empty:
            output_dir = os.path.join("data", 'JP', self.stock_code)
            self.reporter.generate_report(
                df_analysis=self.analysis_result,
                symbol=self.stock_code,
                market='JP',
                metrics=self.raw_data['metrics'],
                output_dir=output_dir
            )
        else:
            print("No analysis result to generate report.")

import pandas as pd # Import needed for the placeholder DataFrames
