#!/usr/bin/env python3
"""
RSK World
Founder: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Phone: +91 93305 39277
Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
Website: https://rskworld.in/

Example script for event detection and analysis using the Sports Analysis Dataset
"""

import json
from collections import Counter
from datetime import datetime

def load_events(json_path):
    """
    Load event annotations from JSON file
    
    Args:
        json_path: Path to events JSON file
        
    Returns:
        List of event dictionaries
    """
    with open(json_path, 'r') as f:
        return json.load(f)

def analyze_events(events):
    """
    Analyze events and generate statistics
    
    Args:
        events: List of event dictionaries
        
    Returns:
        Dictionary with statistics
    """
    stats = {
        'total_events': len(events),
        'event_types': Counter([e['event_type'] for e in events]),
        'events_by_player': Counter([e.get('player_id', 'unknown') for e in events]),
        'time_range': {
            'start': min([e['timestamp'] for e in events]),
            'end': max([e['timestamp'] for e in events])
        }
    }
    return stats

def filter_events_by_type(events, event_type):
    """
    Filter events by type
    
    Args:
        events: List of event dictionaries
        event_type: Type to filter by
        
    Returns:
        Filtered list of events
    """
    return [e for e in events if e['event_type'] == event_type]

def print_statistics(stats):
    """
    Print event statistics in a readable format
    
    Args:
        stats: Statistics dictionary
    """
    print("\n=== Event Statistics ===")
    print(f"Total Events: {stats['total_events']}")
    print("\nEvents by Type:")
    for event_type, count in stats['event_types'].items():
        print(f"  {event_type}: {count}")
    print("\nTop Players by Events:")
    for player_id, count in stats['events_by_player'].most_common(5):
        print(f"  Player {player_id}: {count} events")
    print(f"\nTime Range: {stats['time_range']['start']} to {stats['time_range']['end']}")

def main():
    """
    Main function to run event analysis example
    """
    # Example path (adjust based on your dataset structure)
    events_path = '../data/annotations/events_001.json'
    
    print("Loading event data...")
    events = load_events(events_path)
    
    print("Analyzing events...")
    stats = analyze_events(events)
    print_statistics(stats)
    
    # Example: Filter goals
    print("\n=== Goals ===")
    goals = filter_events_by_type(events, 'goal')
    print(f"Total Goals: {len(goals)}")
    for goal in goals:
        print(f"  Player {goal.get('player_id', 'unknown')} at {goal['timestamp']}")

if __name__ == '__main__':
    main()

