from .base_strategy import BaseStrategy
from fetchers.factory import FetcherFactory
from analysis.us_analyzer import US_Analyzer
from reporting.us_report_generator import US_ReportGenerator
from storage.file_io import DataStorage
import os

class US_Strategy(BaseStrategy):
    def __init__(self, stock_code, av_key):
        super().__init__(stock_code)
        self.av_key = av_key
        self.fetcher = FetcherFactory.get_fetcher('US', av_key=self.av_key)
        self.analyzer = US_Analyzer()
        self.reporter = US_ReportGenerator()
        self.storage = DataStorage()
        self.raw_data = {}
        self.analysis_result = None

    def fetch_data(self):
        print(f"Fetching data for US market, stock: {self.stock_code}")
        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)
        self.raw_data['metrics'] = self.fetcher.get_market_metrics(self.stock_code)


    def analyze_data(self):
        print(f"Analyzing data for US 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']
        )

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