"""
DataSummarizer
Generated by Eden via recursive self-improvement
2025-10-27 23:45:27.839926
"""

class DataSummarizer:
    """
    A class to analyze and summarize structured datasets.
    
    Methods:
        - __init__: Initializes with the dataset
        - summarize: Generates a summary of the dataset
        - get_key_metrics: Returns important statistical metrics
        - identify_trends: Detects significant trends in data
    """

    def __init__(self, data):
        """
        Initialize the DataSummarizer with a dataset.
        
        Args:
            data (list): A list of dictionaries representing records
        """
        self.data = data

    def summarize(self):
        """
        Generate a concise summary of the dataset.
        
        Returns:
            dict: Contains high-level insights about the data
        """
        if not self.data:
            return {"error": "No data provided"}
            
        metrics = self.get_key_metrics()
        trends = self.identify_trends()
        
        return {
            "summary": f"Dataset contains {len(self.data)} records.",
            "key_metrics": metrics,
            "trends": trends
        }

    def get_key_metrics(self):
        """
        Calculate important statistical metrics from the data.
        
        Returns:
            dict: Contains mean, median, and mode values for numeric fields
        """
        # Extract numeric fields from first record to determine which are numeric
        numeric_fields = []
        try:
            sample_record = self.data[0]
            for field in sample_record.keys():
                if isinstance(sample_record[field], (int, float)):
                    numeric_fields.append(field)
        except IndexError:
            return {}
            
        metrics = {}
        
        # Calculate mean for each numeric field
        for field in numeric_fields:
            values = [record[field] for record in self.data]
            metrics[field] = {
                "mean": sum(values) / len(values),
                "median": sorted(values)[len(values)//2],
                "mode": max(set(values), key=values.count)
            }
            
        return metrics

    def identify_trends(self):
        """
        Identify significant trends in the dataset.
        
        Returns:
            dict: Contains trends per field
        """
        # Find fields with consistent values across records
        trends = {}
        
        for field in self.data[0].keys():
            values = [record[field] for record in self.data]
            
            # Check if all values are the same (indicating a strong trend)
            if all(v == values[0] for v in values):
                trends[field] = f"Strong trend: All records have value {values[0]}"
            elif len(set(values)) == 2:
                trends[field] = f"Trend: Alternates between {min(values)} and {max(values)}"
            else:
                pass  # No clear trend
        
        return trends

# Example usage
if __name__ == "__main__":
    # Sample dataset
    data = [
        {'age': 25, 'income': 50000, 'country': 'USA'},
        {'age': 30, 'income': 60000, 'country': 'Canada'},
        {'age': 25, 'income': 55000, 'country': 'USA'},
        {'age': 35, 'income': 70000, 'country': 'UK'}
    ]
    
    summarizer = DataSummarizer(data)
    summary = summarizer.summarize()
    print("Data Summary:")
    print(summary)