from unittest import TestCase
from datetime import datetime, timedelta
from pygeai.analytics.managers import AnalyticsManager
from pygeai.analytics.responses import NumberOfTokensPerDayResponse
from pygeai.core.common.exceptions import APIResponseError


class TestAnalyticsGetNumberOfTokensPerDayIntegration(TestCase):
    """Integration tests for AnalyticsManager.get_number_of_tokens_per_day method."""

    def setUp(self): 
        self.analytics_manager = AnalyticsManager()

        self.end_date = datetime.now()
        self.valid_start_date = "2025-05-01"
        self.valid_end_date = self.end_date.strftime("%Y-%m-%d")


    def __get_number_of_tokens_per_day(self, start_date=None, end_date=None):
        return self.analytics_manager.get_number_of_tokens_per_day(
            start_date=start_date if start_date is not None else self.valid_start_date,
            end_date=end_date if end_date is not None else self.valid_end_date
        )


    def test_get_number_of_tokens_per_day_valid_date_range(self):
        """Test successful retrieval with valid date range."""
        response = self.__get_number_of_tokens_per_day()

        self.assertIsInstance(
            response, 
            NumberOfTokensPerDayResponse, 
            "Response should be an NumberOfTokensPerDayResponse instance"
        )


    def test_get_number_of_tokens_per_day_single_day_range(self):
        """Test retrieval with single day date range."""
        single_date = self.valid_end_date
        
        response = self.__get_number_of_tokens_per_day(
            start_date=single_date,
            end_date=single_date
        )

        self.assertIsInstance(response, NumberOfTokensPerDayResponse)


    def test_get_number_of_tokens_per_day_invalid_start_date(self):
        """Test error handling with invalid start date format."""
        invalid_date_formats = [
            "2024/01/01", 
            "invalid-date",
            ""
        ]

        for invalid_date in invalid_date_formats:
            with self.subTest(date=invalid_date):
                with self.assertRaises(APIResponseError) as exception:
                    self.__get_number_of_tokens_per_day(
                        start_date=invalid_date,
                        end_date=self.valid_end_date
                    )

                self.assertIn(
                    "startDate is required",
                    str(exception.exception),
                    f"Should raise APIError for invalid start date format: {invalid_date}"
                )


    def test_get_number_of_tokens_per_day_invalid_end_date(self):
        """Test error handling with invalid end date format."""
        invalid_date_formats = [
            "2024/01/01", 
            "invalid-date",
            ""
        ]

        for invalid_date in invalid_date_formats:
            with self.subTest(date=invalid_date):
                with self.assertRaises(APIResponseError) as exception:
                    self.__get_number_of_tokens_per_day(
                        start_date=self.valid_start_date,
                        end_date=invalid_date
                    )

                self.assertIn(
                    "endDate is required",
                    str(exception.exception),
                    f"Should raise APIError for invalid end date format: {invalid_date}"
                )

    
    def test_get_number_of_tokens_per_day_invalid_date_range(self):
        """Test error handling when start date is after end date."""
        # Start date after end date
        future_start_date = (self.end_date + timedelta(days=1)).strftime("%Y-%m-%d")
        response = self.__get_number_of_tokens_per_day(start_date=future_start_date)

        self.assertIsInstance(
            response, 
            NumberOfTokensPerDayResponse, 
            "Response should be an NumberOfTokensPerDayResponse instance"
        )