"""
HabitTracker
Generated by Eden via recursive self-improvement
2025-10-27 18:58:26.300882
"""

class HabitTracker:
    """
    A class to track daily habits and monitor consistency.
    
    Attributes:
        habits (dict): Stores habit data with habit names as keys and 
            dictionaries containing start date and completion dates.
    """

    def __init__(self):
        self.habits = {}

    def add_habit(self, task: str) -> None:
        """
        Add a new habit to track.
        
        Args:
            task (str): The name of the habit to add.
        """
        from datetime import date
        today = date.today().isoformat()
        self.habits[task] = {
            "start_date": today,
            "completion_dates": []
        }

    def complete_habit(self, task: str) -> None:
        """
        Mark a habit as completed for today.
        
        Args:
            task (str): The name of the habit to mark as completed.
        """
        from datetime import date
        today = date.today().isoformat()
        if task in self.habits:
            self.habits[task]["completion_dates"].append(today)

    def view_history(self) -> None:
        """
        Display the history of all tracked habits.
        """
        print("\nHabit History:")
        for habit, data in self.habits.items():
            start_date = data["start_date"]
            completions = data["completion_dates"]
            print(f"\nHabit: {habit}")
            print(f"Started on: {start_date}")
            if len(completions) > 0:
                print("Completed on:")
                for date_str in completions:
                    print(date_str)
            else:
                print("Not yet completed.")

    def calculate_streak(self, task: str) -> int:
        """
        Calculate the current streak for a specific habit.
        
        Args:
            task (str): The name of the habit to check streaks for.
            
        Returns:
            int: Number of consecutive days the habit was completed.
        """
        from datetime import date
        if task not in self.habits:
            return 0
            
        completions = self.habits[task]["completion_dates"]
        last_completion_date = None
        
        streak = 0
        max_streak = 0
        
        # Get today's date
        today = date.today().isoformat()
        
        for date_str in reversed(completions):
            if not last_completion_date:
                last_completion_date = date.fromisoformat(date_str)
                streak = 1
            else:
                current_date = date.fromisoformat(date_str)
                difference = (last_completion_date - current_date).days
                
                if difference == 1:
                    streak += 1
                else:
                    max_streak = max(max_streak, streak)
                    streak = 0
                    
                last_completion_date = current_date
        
        # Check for final streak after loop
        max_streak = max(max_streak, streak)
        
        return max_streak

# Example usage:
if __name__ == "__main__":
    tracker = HabitTracker()
    
    # Add some habits
    tracker.add_habit("Drink water")
    tracker.add_habit("Exercise daily")
    tracker.add_habit("Read 30 minutes")
    
    # Complete some days
    import datetime
    today = datetime.date.today().isoformat()
    
    for _ in range(5):
        tracker.complete_habit("Drink water")
        
    for _ in range(2):
        tracker.complete_habit("Exercise daily")
        
    tracker.view_history()
    
    print("\nStreaks:")
    print(f"Drink water: {tracker.calculate_streak('Drink water')}")
    print(f"Exercise daily: {tracker.calculate_streak('Exercise daily')}")
    print(f"Read 30 minutes: {tracker.calculate_streak('Read 30 minutes')}")