File size: 24,898 Bytes
0b3ef70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
"""
Step 1: Preprocess eventing data from Excel files.

This script:
1. Reads Excel files organized by season folders
2. Applies xG, xGoT, xA, xT models
3. Adds formations and team info
4. Calculates match state (score tracking)
5. Outputs a consolidated CSV

Input:
    - Path to league folder containing season subfolders
    - Each season folder contains:
        - Excel files with 'Eventos' sheet
        - team_ids.json with team mappings

Output:
    - eventing_consolidado.csv
"""

import os
import json
import pickle
import pandas as pd
import numpy as np
from pathlib import Path
from tqdm import tqdm
import ast

from .utils import load_config, get_pipeline_root, ensure_output_dir

# Note: We intentionally do NOT suppress warnings. 
# All warnings should be visible for debugging.


def load_models(pipeline_root: Path) -> dict:
    """Load all ML models for xG, xGoT, xA calculations.
    
    Raises:
        FileNotFoundError: If any model file is missing.
        pickle.UnpicklingError: If model files are corrupted.
    """
    models_dir = pipeline_root / 'models'
    
    if not models_dir.exists():
        raise FileNotFoundError(f"Models directory not found: {models_dir}")
    
    required_models = {
        'xg': 'modelo_goles_esperados_v2.pkl',
        'xgot': 'modelo_goles_esperados_alarco.pkl',
        'xa': 'modelo_asistencias_esperadas_v3.pkl',
    }
    
    # Check all files exist before loading
    for model_name, filename in required_models.items():
        model_path = models_dir / filename
        if not model_path.exists():
            raise FileNotFoundError(f"Required model file not found: {model_path}")
    
    xt_path = models_dir / 'xTGrid.xlsx'
    if not xt_path.exists():
        raise FileNotFoundError(f"Required xT grid file not found: {xt_path}")
    
    models = {}
    
    # Load xG model
    with open(models_dir / 'modelo_goles_esperados_v2.pkl', 'rb') as f:
        models['xg'] = pickle.load(f)
    
    # Load xGoT model
    with open(models_dir / 'modelo_goles_esperados_alarco.pkl', 'rb') as f:
        models['xgot'] = pickle.load(f)
    
    # Load xA model
    with open(models_dir / 'modelo_asistencias_esperadas_v3.pkl', 'rb') as f:
        models['xa'] = pickle.load(f)
    
    # Load xT grid
    models['xt_grid'] = pd.read_excel(models_dir / 'xTGrid.xlsx', header=None).values
    
    return models


def abbreviate(text: str) -> str:
    """Create abbreviation from text (first letter of each word)."""
    return "".join([word[0] for word in str(text).split()])


def add_formations(df: pd.DataFrame, config: dict) -> pd.DataFrame:
    """Add formation information to events.
    
    Raises:
        KeyError: If 'formation_ids' not found in config.
    """
    if 'formation_ids' not in config:
        raise KeyError("'formation_ids' not found in config.yaml. This is required for formation mapping.")
    
    formation_ids = config['formation_ids']
    
    def extract_team_formation(row):
        # Parse qualifiers - if malformed, return None (formations are optional per event)
        if pd.isna(row) or row == '':
            return None
        items = ast.literal_eval(row)
        return next((item["value"] for item in items 
                    if item.get("type", {}).get("value") == 130), None)
    
    df["id_formation"] = df["qualifiers"].apply(extract_team_formation)
    df["id_formation"] = df["id_formation"].map(formation_ids)
    
    df = df.sort_values(by=['Competencia', 'Temporada', 'matchId', 'period_id', 'minute', 'second'])
    df['id_formation'] = df.groupby(['Competencia', 'Temporada', 'matchId', "teamId"])["id_formation"].ffill()
    
    # Build rival formation mapping
    formaciones_rival_extendida = []
    formation_groups = df[df["id_formation"].notna()].groupby(
        ["Competencia", "Temporada", "matchId", "period_id", "teamId"]
    )
    
    for (comp, temp, match, period, team), group in formation_groups:
        min_minuto = group["minute"].min()
        max_minuto = group["minute"].max()
        
        minutos = pd.DataFrame({
            "Competencia": comp,
            "Temporada": temp,
            "matchId": match,
            "period_id": period,
            "equipo vs": team,
            "minute": range(int(min_minuto), int(max_minuto) + 1)
        })
        
        formaciones = group[["Competencia", "Temporada", "matchId", "period_id", 
                            "minute", "teamId", "id_formation"]].drop_duplicates()
        formaciones = formaciones.rename(columns={
            "teamId": "equipo vs",
            "id_formation": "id_formation_rival"
        })
        
        df_merge = minutos.merge(
            formaciones, 
            on=["Competencia", "Temporada", "matchId", "period_id", "minute", "equipo vs"], 
            how="left"
        )
        df_merge["id_formation_rival"] = df_merge["id_formation_rival"].ffill()
        formaciones_rival_extendida.append(df_merge)
    
    if formaciones_rival_extendida:
        formaciones_rival_completa = pd.concat(formaciones_rival_extendida, ignore_index=True)
        df = df.merge(
            formaciones_rival_completa,
            on=['Competencia', 'Temporada', 'matchId', 'period_id', 'minute', 'equipo vs'],
            how='left'
        ).drop_duplicates("id")
    
    return df


def calculate_xg(df: pd.DataFrame, model) -> pd.DataFrame:
    """Calculate expected goals (xG) for shots."""
    shots = df[df['isShot'] == 1].copy()
    if len(shots) == 0:
        df['xG'] = np.nan
        return df
    
    shots['start_x'] = shots['x']
    shots['start_y'] = shots['y']
    shots = shots.drop_duplicates(subset=['start_x', 'start_y', 'playerId', 'minute', 'second'])
    shots_copy = df[df['isShot'] == 1].drop_duplicates(subset=['x', 'y', 'playerId', 'minute', 'second'])
    
    shots['isGoal'] = shots['isGoal'].fillna(0)
    shots['Distance'] = np.sqrt((100 - shots['start_x'])**2 + (50 - shots['start_y'])**2)
    shots['angulo'] = np.arctan(7.32 * shots['start_x'] / 
                                (shots['start_x']**2 + shots['start_y']**2 - (7.32/2)**2))
    
    # Filter own goals if column exists
    if "isOwnGoal" in shots.columns:
        shots = shots[shots['isOwnGoal'] != 1]
    
    # Prepare features
    shots_features = shots[['start_x', 'start_y', 'Distance', 'angulo', 
                           'qualifiers', 'satisfiedEventsTypes', 'isGoal']].reset_index(drop=True)
    
    # Pattern of play
    shots_features['patron_juego'] = np.select(
        [
            shots_features.qualifiers.str.contains('ThrowIn|ThrowinSetPiece', na=False),
            shots_features.qualifiers.str.contains('CornerTaken|FromCorner', na=False),
            shots_features.qualifiers.str.contains('Freekick', na=False),
            shots_features.qualifiers.str.contains('RegularPlay', na=False),
            shots_features.qualifiers.str.contains('FastBreak', na=False),
            shots_features.qualifiers.str.contains('Penalty', na=False),
            shots_features.qualifiers.str.contains('Cross', na=False),
            shots_features.qualifiers.str.contains('SetPiece', na=False),
        ],
        [
            "Pelota Parada - Lateral",
            "Pelota Parada - Corner",
            "Pelota Parada - Tiro Libre",
            "Jugada Abierta",
            "Jugada Abierta - Contraataque",
            "Pelota Parada - Penal",
            "Jugada Abierta - Centro",
            "Pelota Parada",
        ],
        default="Jugada Abierta"
    )
    
    # Area
    shots_features['satisfiedEventsTypes'] = shots_features['satisfiedEventsTypes'].astype(str)
    shots_features['area'] = np.select(
        [
            shots_features.satisfiedEventsTypes.str.contains(', 1,', na=False),
            shots_features.satisfiedEventsTypes.str.contains(', 0,', na=False),
        ],
        ["Area penal", "Area chica"],
        default="Fuera area"
    )
    
    # Shot type
    shots_features['tipo_tiro'] = np.select(
        [
            shots_features.qualifiers.str.contains('Head', na=False),
            shots_features.qualifiers.str.contains('OtherBodyPart', na=False),
            shots_features.qualifiers.str.contains('Foot', na=False),
        ],
        ["Cabeza", "Otro", "Pierna"],
        default="0"
    )
    
    shots_features['gran_chance'] = shots_features.qualifiers.str.contains('BigChance', na=False)
    shots_features['asistido'] = shots_features.qualifiers.str.contains('IntentionalAssist', na=False)
    
    # Select model features
    model_features = shots_features[['start_x', 'start_y', 'Distance', 'angulo', 
                                     'patron_juego', 'tipo_tiro', 'area', 
                                     'gran_chance', 'asistido']].dropna()
    
    if len(model_features) == 0:
        df['xG'] = np.nan
        return df
    
    # Predict
    probs = model.predict_proba(model_features)[:, 1]
    probs_df = pd.DataFrame({'xG': probs})
    
    shots_with_prob = pd.concat([shots_copy.reset_index(drop=True), probs_df], axis=1)
    df = df.merge(shots_with_prob[['id', 'xG']], on='id', how='left')
    
    return df


def calculate_xgot(df: pd.DataFrame, model) -> pd.DataFrame:
    """Calculate expected goals on target (xGoT)."""
    if 'goalMouthY' not in df.columns or 'goalMouthZ' not in df.columns:
        df['xGoT'] = np.nan
        return df
    
    shots = df[['id', 'isGoal', 'goalMouthY', 'goalMouthZ', 'xG']].copy()
    on_target = shots[
        (shots['goalMouthY'] > 45.2) & 
        (shots['goalMouthY'] < 54.8) & 
        (shots['goalMouthZ'] < 38)
    ].copy()
    
    if len(on_target) == 0:
        df['xGoT'] = np.nan
        return df
    
    on_target['DistanceY'] = np.sqrt((50 - on_target['goalMouthY'])**2)
    on_target['DistanceZ'] = np.sqrt((0 - on_target['goalMouthZ'])**2)
    on_target['xG'] = on_target['xG'].fillna(0)
    
    model_features = on_target[['xG', 'DistanceY', 'DistanceZ']]
    
    probs = model.predict_proba(model_features)[:, 1]
    on_target['xGoT'] = probs
    
    df = df.merge(on_target[['id', 'xGoT']], on='id', how='left')
    
    return df


def calculate_xa(df: pd.DataFrame, model) -> pd.DataFrame:
    """Calculate expected assists (xA) for passes."""
    passes = df[(df['event_name'] == 'Pass') & (df['outcome_type'] == 'Successful')].copy()
    
    if len(passes) == 0:
        df['xA'] = np.nan
        return df
    
    # Pattern of play
    passes['patron_juego'] = np.select(
        [
            passes.qualifiers.str.contains('ThrowIn|ThrowinSetPiece', na=False),
            passes.qualifiers.str.contains('CornerTaken|FromCorner', na=False),
            passes.qualifiers.str.contains('FreekickTaken', na=False),
            passes.qualifiers.str.contains('RegularPlay', na=False),
            passes.qualifiers.str.contains('FastBreak', na=False),
            passes.qualifiers.str.contains('GoalKick', na=False),
            passes.qualifiers.str.contains('Cross', na=False),
            passes.qualifiers.str.contains('SetPiece', na=False),
        ],
        [
            "Pelota Parada - Lateral",
            "Pelota Parada - Corner",
            "Pelota Parada - Tiro Libre",
            "Jugada Abierta",
            "Jugada Abierta - Contraataque",
            "Pelota Parada - Saque de arco",
            "Jugada Abierta - Centro",
            "Pelota Parada",
        ],
        default="Jugada Abierta"
    )
    
    # Pass type
    passes['tipo_pase'] = np.select(
        [
            passes.qualifiers.str.contains('Throughball', na=False),
            passes.qualifiers.str.contains('Cross', na=False),
            passes.qualifiers.str.contains('Chipped', na=False),
            passes.qualifiers.str.contains('Longball', na=False),
        ],
        ["Pase filtrado", "Centro", "Pase alto", "Pase largo raso"],
        default="Pase raso"
    )
    
    # Zone
    passes['zona'] = np.select(
        [
            passes.qualifiers.str.contains("'Zone'}, 'value': 'Back'", na=False),
            passes.qualifiers.str.contains("'Zone'}, 'value': 'Center'", na=False),
            passes.qualifiers.str.contains("'Zone'}, 'value': 'Left'", na=False),
            passes.qualifiers.str.contains("'Zone'}, 'value': 'Right'", na=False),
        ],
        ["Atras", "Centro campo", "Izquierda", "Derecha"],
        default="0"
    )
    
    passes['parte_cuerpo'] = np.select(
        [
            passes.qualifiers.str.contains('HeadPass', na=False),
            passes.qualifiers.str.contains('OtherBodyPart', na=False),
        ],
        ["Cabeza", "Otro"],
        default="Pie"
    )
    
    passes['Distance'] = np.sqrt((passes['endX'] - passes['x'])**2 + 
                                 (passes['endY'] - passes['y'])**2)
    
    model_features = passes[['x', 'y', 'endX', 'endY', 'Distance', 
                            'patron_juego', 'tipo_pase', 'zona', 'parte_cuerpo']]
    
    probs = model.predict_proba(model_features)[:, 1]
    passes['xA'] = probs
    
    df = df.merge(passes[['id', 'xA']], on='id', how='left')
    
    return df


def calculate_xt(df: pd.DataFrame, xt_grid: np.ndarray) -> pd.DataFrame:
    """Calculate expected threat (xT) for passes."""
    xT_rows, xT_cols = xt_grid.shape
    
    passes = df[df['event_name'] == 'Pass'].copy()
    passes = passes.dropna(subset=['x', 'y', 'endX', 'endY'])
    passes = passes[passes['outcome_type'] == 'Successful']
    
    if len(passes) == 0:
        df['xT'] = np.nan
        return df
    
    # Bin coordinates
    passes['x1_bin'] = pd.cut(passes['x'], bins=xT_cols, labels=False).astype(int)
    passes['y1_bin'] = pd.cut(passes['y'], bins=xT_rows, labels=False).astype(int)
    passes['x2_bin'] = pd.cut(passes['endX'], bins=xT_cols, labels=False).astype(int)
    passes['y2_bin'] = pd.cut(passes['endY'], bins=xT_rows, labels=False).astype(int)
    
    # Calculate xT
    passes['start_zone_value'] = passes.apply(
        lambda row: xt_grid[int(row['y1_bin']), int(row['x1_bin'])] 
        if pd.notna(row['y1_bin']) and pd.notna(row['x1_bin']) else 0, 
        axis=1
    )
    passes['end_zone_value'] = passes.apply(
        lambda row: xt_grid[int(row['y2_bin']), int(row['x2_bin'])] 
        if pd.notna(row['y2_bin']) and pd.notna(row['x2_bin']) else 0, 
        axis=1
    )
    passes['xT'] = passes['end_zone_value'] - passes['start_zone_value']
    
    df = df.merge(passes[['id', 'xT']], on='id', how='left')
    
    return df


def load_excel_files(base_path: Path, league: str) -> pd.DataFrame:
    """Load all Excel files from season folders.
    
    Raises:
        FileNotFoundError: If base_path doesn't exist or contains no season folders.
        ValueError: If no Excel files found or if required files are missing.
    """
    if not base_path.exists():
        raise FileNotFoundError(f"Input folder not found: {base_path}")
    
    all_dfs = []
    errors = []
    
    season_folders = [f for f in os.listdir(base_path) if (base_path / f).is_dir()]
    
    if not season_folders:
        raise FileNotFoundError(f"No season folders found in {base_path}. Expected subfolders like '2023-24', '2024-25', etc.")
    
    for folder_name in season_folders:
        folder_path = base_path / folder_name
        
        # Find Excel files
        files = [f for f in os.listdir(folder_path) 
                if f.endswith('.xlsx') or f.endswith('.xls')]
        
        if not files:
            raise ValueError(f"No Excel files (.xlsx/.xls) found in season folder: {folder_path}")
        
        # Load team mappings - REQUIRED
        json_path = folder_path / "team_ids.json"
        
        if not json_path.exists():
            raise FileNotFoundError(
                f"Required file 'team_ids.json' not found in {folder_path}. "
                "This file is required for team name mapping."
            )
        
        with open(json_path, "r", encoding="utf-8") as f:
            team_data = json.load(f)
        df_teams = pd.DataFrame(team_data)
        
        if 'homeTeamId' not in df_teams.columns or 'homeTeamName' not in df_teams.columns:
            raise ValueError(
                f"team_ids.json in {folder_path} must contain 'homeTeamId' and 'homeTeamName' columns. "
                f"Found columns: {list(df_teams.columns)}"
            )
        
        df_teams = df_teams.rename(columns={
            'homeTeamId': 'teamId', 
            'homeTeamName': 'TeamName'
        })
        
        # Process each Excel file
        for f in tqdm(files, desc=f'  Processing {folder_name}', leave=False):
            file_path = folder_path / f
            
            # Check sheet exists
            excel_file = pd.ExcelFile(file_path)
            if 'Eventos' not in excel_file.sheet_names:
                raise ValueError(
                    f"Excel file {file_path} does not contain required sheet 'Eventos'. "
                    f"Available sheets: {excel_file.sheet_names}"
                )
            
            df = pd.read_excel(file_path, sheet_name='Eventos')
            
            # Validate required columns exist
            required_cols = ['teamId', 'equipo vs', 'event_name', 'x', 'y', 'minute', 'second']
            missing_cols = [col for col in required_cols if col not in df.columns]
            if missing_cols:
                raise ValueError(
                    f"Excel file {file_path} is missing required columns: {missing_cols}. "
                    f"Available columns: {list(df.columns)}"
                )
            
            df['Competencia'] = league
            df['Temporada'] = folder_name
            
            df = df.merge(df_teams, on='teamId', how='inner')
            df = df.merge(
                df_teams.rename(columns={'TeamName': 'TeamRival'}),
                left_on='equipo vs', right_on='teamId',
                how='inner'
            ).drop(columns=['teamId_y'])
            df = df.rename(columns={'teamId_x': 'teamId'})
            
            if len(df) == 0:
                raise ValueError(
                    f"No events matched after merging with team_ids.json for {file_path}. "
                    "Check that teamId values in the Excel file exist in team_ids.json."
                )
            
            all_dfs.append(df)
    
    if not all_dfs:
        raise ValueError(f"No valid data found in {base_path}")
    
    return pd.concat(all_dfs, ignore_index=True)


def clean_events(df: pd.DataFrame) -> pd.DataFrame:
    """Clean and filter events."""
    # Fix period_id
    df['period_id'] = np.where(df['period_id'] == 16, 1, df['period_id'])
    
    # Filter unwanted events
    unwanted_events = [
        'Challenge', 'CornerAwarded', 'SubstitutionOff', 
        'SubstitutionOn', 'FormationChange', 'Card'
    ]
    
    df = df[~df['event_name'].isin(unwanted_events)]
    
    # Filter by satisfiedEventsTypes
    if 'satisfiedEventsTypes' in df.columns:
        df['satisfiedEventsTypes'] = df['satisfiedEventsTypes'].astype(str)
        df = df[~df['satisfiedEventsTypes'].str.contains('198', na=False)]
    
    # Filter to first and second half only
    df = df[df['period_id'].isin([1, 2])]
    
    # Remove unsuccessful aerials
    df = df[~((df['event_name'] == 'Aerial') & (df['outcome_type'] == 'Unsuccessful'))]
    
    return df


def add_possession_chains(df: pd.DataFrame, league: str) -> pd.DataFrame:
    """Add possession chain identifiers."""
    df = df.sort_values(['matchId', 'period_id', 'minute', 'second'])
    
    df['lag_team'] = df['teamId'].shift(1)
    df['mismo equipo'] = df['teamId'] == df['lag_team']
    df['Posesion'] = (~df['mismo equipo']).cumsum()
    df['Posesion'] = df['Posesion'].astype(str) + f"_{abbreviate(league)}_" + df['Competencia'].astype(str)
    
    return df


def add_time_features(df: pd.DataFrame) -> pd.DataFrame:
    """Add time-based features."""
    df['time_seconds'] = df['minute'] * 60 + df['second']
    df = df.sort_values(by=['matchId', 'period_id', 'time_seconds'])
    
    df['time_since_previous_action'] = df.groupby(['matchId', 'period_id'])['time_seconds'].diff()
    df['previous_event'] = df.groupby(['matchId', 'period_id'])['event_name'].shift()
    df['next_event_posesion'] = df.groupby(['matchId', 'period_id'])['event_name'].shift(-1)
    
    return df


def add_match_state(df: pd.DataFrame) -> pd.DataFrame:
    """Add match state (score tracking) features."""
    df = df.sort_values(['matchId', 'time_seconds', 'eventId'])
    
    df['isGoal'] = df['isGoal'].fillna(False)
    df['goal_int'] = df['isGoal'].astype(int)
    
    df['goles_equipo'] = df.groupby(['matchId', 'teamId'])['goal_int'].cumsum()
    df['goles_totales'] = df.groupby('matchId')['goal_int'].cumsum()
    df['goles_rival'] = df['goles_totales'] - df['goles_equipo']
    
    df['estado_partido'] = np.select(
        [
            df['goles_equipo'] > df['goles_rival'],
            df['goles_equipo'] < df['goles_rival']
        ],
        ['Ganando', 'Perdiendo'],
        default='Empate'
    )
    
    return df


def preprocess_league(
    input_folder: Path,
    league: str,
    output_folder: Path
) -> Path:
    """
    Main preprocessing function for a league.
    
    Args:
        input_folder: Path to league data folder
        league: League name
        output_folder: Output directory
        
    Returns:
        Path to output CSV file
    """
    print(f"\n{'='*80}")
    print(f"STEP 1: PREPROCESSING - {league}")
    print(f"{'='*80}")
    
    pipeline_root = get_pipeline_root()
    config = load_config()
    
    # Load models
    print("\n📦 Loading ML models...")
    models = load_models(pipeline_root)
    print("  ✅ Models loaded")
    
    # Load Excel files
    print(f"\n📂 Loading data from {input_folder}...")
    df = load_excel_files(input_folder, league)
    print(f"  ✅ Loaded {len(df):,} events from {df['matchId'].nunique()} matches")
    
    # Clean events
    print("\n🧹 Cleaning events...")
    df = clean_events(df)
    print(f"  ✅ {len(df):,} events after cleaning")
    
    # Add unique IDs
    df = df.reset_index(drop=True)
    if 'id' not in df.columns or df['id'].isna().any():
        league_abbr = abbreviate(league)
        df['id'] = [f"{league_abbr}_{df['Competencia'].iloc[i]}_{i}" 
                   for i in range(len(df))]
    
    # Add possession chains
    print("\n🔗 Adding possession chains...")
    df = add_possession_chains(df, league)
    
    # Add time features
    print("\n⏱️ Adding time features...")
    df = add_time_features(df)
    
    # Add formations
    print("\n📋 Adding formations...")
    df = add_formations(df, config)
    
    # Calculate xG
    print("\n⚽ Calculating xG...")
    df = calculate_xg(df, models['xg'])
    xg_count = df['xG'].notna().sum()
    print(f"  ✅ xG calculated for {xg_count:,} shots")
    
    # Calculate xGoT
    print("\n🥅 Calculating xGoT...")
    df = calculate_xgot(df, models['xgot'])
    xgot_count = df['xGoT'].notna().sum()
    print(f"  ✅ xGoT calculated for {xgot_count:,} shots on target")
    
    # Calculate xA
    print("\n🎯 Calculating xA...")
    df = calculate_xa(df, models['xa'])
    xa_count = df['xA'].notna().sum()
    print(f"  ✅ xA calculated for {xa_count:,} passes")
    
    # Calculate xT
    print("\n📈 Calculating xT...")
    df = calculate_xt(df, models['xt_grid'])
    xt_count = df['xT'].notna().sum()
    print(f"  ✅ xT calculated for {xt_count:,} passes")
    
    # Add match state
    print("\n📊 Adding match state...")
    df = add_match_state(df)
    
    # Save output
    ensure_output_dir(output_folder)
    league_filename = league.replace(" ", "_").replace("/", "-")
    output_path = output_folder / f"{league_filename}_eventing.csv"
    df.to_csv(output_path, index=False)
    
    print(f"\n{'='*80}")
    print(f"✅ STEP 1 COMPLETE")
    print(f"  Output: {output_path}")
    print(f"  Total events: {len(df):,}")
    print(f"  Matches: {df['matchId'].nunique()}")
    print(f"  Teams: {df['TeamName'].nunique()}")
    print(f"{'='*80}")
    
    return output_path


if __name__ == "__main__":
    import argparse
    
    # Default output folder: corner_kick_pipeline/datasets/raw
    default_output = Path(__file__).parent.parent / "datasets" / "raw"
    
    parser = argparse.ArgumentParser(description="Preprocess eventing data")
    parser.add_argument("--input-folder", required=True, help="Path to league data folder")
    parser.add_argument("--league", required=True, help="League name")
    parser.add_argument("--output-folder", type=Path, default=default_output, help="Output directory (default: racing_tools/datasets/raw)")
    
    args = parser.parse_args()
    
    preprocess_league(
        input_folder=Path(args.input_folder),
        league=args.league,
        output_folder=args.output_folder
    )