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37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
"""
Utility functions for the corner kick analysis pipeline.

This module provides shared functions for:
- Qualifier parsing
- State tuple handling
- Input validation
- Configuration loading
"""

import ast
import json
import yaml
import pandas as pd
import numpy as np
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any, Union


def load_config(config_path: Optional[Path] = None) -> Dict:
    """
    Load pipeline configuration from YAML file.
    
    Args:
        config_path: Path to config file. If None, uses default location.
        
    Returns:
        Configuration dictionary
        
    Raises:
        FileNotFoundError: If config file doesn't exist.
        yaml.YAMLError: If config file is malformed.
    """
    if config_path is None:
        config_path = Path(__file__).parent.parent / "config.yaml"
    
    if not config_path.exists():
        raise FileNotFoundError(
            f"Configuration file not found: {config_path}. "
            "Ensure config.yaml exists in the corner_kick_pipeline directory."
        )
    
    with open(config_path, 'r', encoding='utf-8') as f:
        config = yaml.safe_load(f)
    
    if config is None:
        raise ValueError(f"Configuration file is empty: {config_path}")
    
    return config


def get_pipeline_root() -> Path:
    """Get the root directory of the pipeline."""
    return Path(__file__).parent.parent


# =============================================================================
# QUALIFIER PARSING
# =============================================================================

def parse_qualifiers(qualifiers_str: str) -> List[Dict]:
    """
    Parse qualifier string from event data.
    
    Args:
        qualifiers_str: String representation of qualifiers list
        
    Returns:
        List of qualifier dictionaries
        
    Raises:
        ValueError: If qualifiers_str is malformed and cannot be parsed.
    """
    if pd.isna(qualifiers_str) or qualifiers_str == '':
        return []
    
    # Try ast.literal_eval first (handles Python-style lists)
    try:
        result = ast.literal_eval(qualifiers_str)
        if isinstance(result, list):
            return result
        raise ValueError(f"Parsed result is not a list: {type(result)}")
    except (ValueError, SyntaxError) as e:
        # Try JSON parsing as fallback
        try:
            result = json.loads(qualifiers_str)
            if isinstance(result, list):
                return result
            raise ValueError(f"Parsed JSON is not a list: {type(result)}")
        except json.JSONDecodeError as json_error:
            raise ValueError(
                f"Failed to parse qualifiers string. Not valid Python literal or JSON.\n"
                f"String: {qualifiers_str[:200]}...\n"
                f"AST error: {e}\n"
                f"JSON error: {json_error}"
            )


def has_qualifier(event_qualifiers: List[Dict], qualifier_name: str) -> bool:
    """
    Check if an event has a specific qualifier.
    
    Args:
        event_qualifiers: List of qualifier dictionaries
        qualifier_name: Name of qualifier to check for
        
    Returns:
        True if qualifier is present
    """
    if not event_qualifiers:
        return False
    
    for q in event_qualifiers:
        if isinstance(q, dict):
            q_type = q.get('type', {})
            if isinstance(q_type, dict):
                display_name = q_type.get('displayName', '')
                if display_name == qualifier_name:
                    return True
    return False


def get_qualifier_value(event_qualifiers: List[Dict], qualifier_name: str) -> Optional[Any]:
    """
    Get the value of a specific qualifier.
    
    Args:
        event_qualifiers: List of qualifier dictionaries
        qualifier_name: Name of qualifier
        
    Returns:
        Qualifier value or None if not found
    """
    if not event_qualifiers:
        return None
    
    for q in event_qualifiers:
        if isinstance(q, dict):
            q_type = q.get('type', {})
            if isinstance(q_type, dict):
                display_name = q_type.get('displayName', '')
                if display_name == qualifier_name:
                    return q.get('value')
    return None


# =============================================================================
# STATE TUPLE HANDLING
# =============================================================================

def parse_tuple_string(tuple_str: str) -> Tuple:
    """
    Parse a string representation of a tuple.
    
    Args:
        tuple_str: String like "('zone', 'event', 'team')"
        
    Returns:
        Actual tuple
        
    Raises:
        ValueError: If the string cannot be parsed as a tuple.
    """
    try:
        result = ast.literal_eval(tuple_str)
        if isinstance(result, tuple):
            return result
        raise ValueError(f"Parsed result is not a tuple: {type(result)}")
    except (ValueError, SyntaxError) as e:
        # Try manual parsing for edge cases
        tuple_str = str(tuple_str).strip("'\"")
        if tuple_str.startswith('(') and tuple_str.endswith(')'):
            tuple_str = tuple_str[1:-1]
            parts = [p.strip().strip("'\"") for p in tuple_str.split(',')]
            if len(parts) >= 1:
                return tuple(parts)
        
        raise ValueError(
            f"Failed to parse tuple string: '{tuple_str}'. "
            f"Expected format: \"('zone', 'event', 'team')\". Error: {e}"
        )


def create_state_tuple(zone: str, event_type: str, team_type: str) -> Tuple[str, str, str]:
    """
    Create a standardized state tuple.
    
    Args:
        zone: Zone name or special state (CORNER, ABSORCION)
        event_type: Event type
        team_type: 'atacante' or 'defensor'
        
    Returns:
        State tuple
    """
    return (zone, event_type, team_type)


def is_absorption_state(state: Union[Tuple, str]) -> bool:
    """
    Check if a state is an absorption (terminal) state.
    
    Args:
        state: State tuple or string
        
    Returns:
        True if absorption state
    """
    if isinstance(state, tuple) and len(state) > 0:
        return state[0] == 'ABSORCION'
    elif isinstance(state, str):
        return state.startswith('ABSORCION')
    return False


def get_absorption_type(state: Union[Tuple, str]) -> Optional[str]:
    """
    Extract the absorption type from a state.
    
    Args:
        state: Absorption state tuple
        
    Returns:
        Absorption type (e.g., 'gol', 'perdida_posesion')
    """
    if isinstance(state, tuple) and len(state) > 1:
        return state[1]
    return None


# =============================================================================
# INPUT VALIDATION
# =============================================================================

REQUIRED_EVENTING_COLUMNS = [
    'event_name', 'qualifiers', 'x', 'y', 'teamId', 'playerId', 
    'matchId', 'period_id', 'minute', 'second', 'jugador', 
    'TeamName', 'TeamRival'
]

REQUIRED_SUMMARY_COLUMNS = [
    'corner_sequence_id', 'matchId', 'TeamName', 'TeamRival',
    'absorption_event', 'sequence_length'
]

REQUIRED_DETAIL_COLUMNS = [
    'corner_sequence_id', 'event_type', 'origin_zone', 
    'is_attacking_team', 'event_index'
]


def validate_eventing_csv(df: pd.DataFrame) -> Tuple[bool, List[str]]:
    """
    Validate that eventing CSV has required columns.
    
    Args:
        df: DataFrame to validate
        
    Returns:
        Tuple of (is_valid, missing_columns)
    """
    missing = [col for col in REQUIRED_EVENTING_COLUMNS if col not in df.columns]
    return len(missing) == 0, missing


def validate_summary_csv(df: pd.DataFrame) -> Tuple[bool, List[str]]:
    """
    Validate that summary CSV has required columns.
    
    Args:
        df: DataFrame to validate
        
    Returns:
        Tuple of (is_valid, missing_columns)
    """
    missing = [col for col in REQUIRED_SUMMARY_COLUMNS if col not in df.columns]
    return len(missing) == 0, missing


def validate_detail_csv(df: pd.DataFrame) -> Tuple[bool, List[str]]:
    """
    Validate that detail CSV has required columns.
    
    Args:
        df: DataFrame to validate
        
    Returns:
        Tuple of (is_valid, missing_columns)
    """
    missing = [col for col in REQUIRED_DETAIL_COLUMNS if col not in df.columns]
    return len(missing) == 0, missing


# =============================================================================
# DATA PROCESSING HELPERS
# =============================================================================

def safe_str(value: Any, default: str = '') -> str:
    """
    Safely convert a value to string.

    Args:
        value: Value to convert
        default: Default if value is None or NaN

    Returns:
        String value
    """
    if value is None or (isinstance(value, float) and pd.isna(value)):
        return default
    if pd.isna(value):
        return default
    return str(value)


def safe_float(value: Any, default: float = 0.0) -> float:
    """
    Safely convert a value to float.

    Args:
        value: Value to convert
        default: Default if conversion fails

    Returns:
        Float value
    """
    if value is None or (isinstance(value, float) and pd.isna(value)):
        return default
    try:
        result = float(value)
        return default if pd.isna(result) else result
    except (ValueError, TypeError):
        return default


def safe_int(value: Any, default: int = 0) -> int:
    """
    Safely convert a value to int.

    Args:
        value: Value to convert
        default: Default if conversion fails

    Returns:
        Int value
    """
    if value is None or (isinstance(value, float) and pd.isna(value)):
        return default
    try:
        return int(float(value))
    except (ValueError, TypeError):
        return default


def normalize_team_name(name: str) -> str:
    """
    Normalize team name for consistent matching.
    
    Args:
        name: Team name
        
    Returns:
        Normalized team name
    """
    if pd.isna(name):
        return ""
    return str(name).strip().lower()


def format_sequence_id(match_id: int, event_id: int, minute: int, second: float) -> str:
    """
    Create a unique sequence identifier.
    
    Args:
        match_id: Match ID
        event_id: Corner event ID
        minute: Minute of corner
        second: Second of corner
        
    Returns:
        Unique sequence ID string
    """
    return f"{match_id}_{event_id}_{minute}_{second}"


# =============================================================================
# OUTPUT HELPERS
# =============================================================================

def ensure_output_dir(output_path: Path) -> None:
    """
    Ensure output directory exists.
    
    Args:
        output_path: Path to output directory or file
    """
    if output_path.suffix:
        # It's a file path, get parent
        output_path.parent.mkdir(parents=True, exist_ok=True)
    else:
        # It's a directory
        output_path.mkdir(parents=True, exist_ok=True)


def save_dataframe(df: pd.DataFrame, path: Path, index: bool = False) -> None:
    """
    Save DataFrame to CSV with standard settings.
    
    Args:
        df: DataFrame to save
        path: Output path
        index: Whether to include index
    """
    ensure_output_dir(path)
    df.to_csv(path, index=index)
    print(f"  ✅ Saved: {path} ({len(df):,} rows)")


def save_json(data: Dict, path: Path) -> None:
    """
    Save dictionary to JSON file.
    
    Args:
        data: Dictionary to save
        path: Output path
    """
    ensure_output_dir(path)
    with open(path, 'w', encoding='utf-8') as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
    print(f"  ✅ Saved: {path}")