File size: 20,592 Bytes
62f41ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Scouting Score System
=====================
Scores South American players combining:
- Performance score (60%): weighted metrics per position from PESOS xlsx
- Profile score (40%): age, EU nationality, league tier, market value

Usage:
    python scouting_score.py [--min-minutes 450] [--output scouting_scores.csv]
"""

import re
import sys
import argparse
import pandas as pd
import numpy as np
import openpyxl
from pathlib import Path

BASE_DIR = Path(__file__).parent
# Default: read the player-season grain from the new pipeline.
# --legacy-csv flag still allows reading the old per-row CSV for comparison.
PARQUET_PATH = BASE_DIR / 'data' / 'clean' / 'player_season_enriched.parquet'
CSV_PATH = BASE_DIR / 'player_stats_p90_all_leagues.csv'
WEIGHTS_PATH = BASE_DIR / 'PESOS POR VARIABLE (BLOQUES).xlsx'
OUTPUT_PATH = BASE_DIR / 'scouting_scores.csv'

# ==========================================
# MAPPINGS & CONFIG
# ==========================================

POSITION_MAP = {
    'GK': 'P1',
    'DL': 'P2', 'DML': 'P2', 'Wing Back Left': 'P2',
    'DR': 'P3', 'DMR': 'P3', 'Wing Back Right': 'P3',
    'DC': 'P4', 'Central Defender Centre': 'P4',
    'AML': 'P7', 'FWL': 'P7', 'ML': 'P7', 'Winger Left': 'P7',
    'DMC': 'P8', 'MC': 'P8',
    'Defensive Midfielder Centre': 'P8', 'Defensive Midfielder Left': 'P8',
    'Defensive Midfielder Right': 'P8', 'Central Midfielder Left': 'P8',
    'Central Midfielder Right': 'P8',
    'FW': 'P9', 'Striker Centre/Right': 'P9', 'Striker Left/Centre': 'P9',
    'Second Striker Centre': 'P9',
    'AMC': 'P10',
    'AMR': 'P11', 'FWR': 'P11', 'MR': 'P11', 'Winger Right': 'P11',
}

METRIC_FIXES = {
    'acciones_defensivas_en_ofensiva_rival': 'acciones_defensivas_en_ofensiva',
    'presion': 'presiones',
}

LEAGUE_TIERS = {
    # Top-5 Europe
    'Spain La Liga': 1.0, 'Spanish La Liga': 1.0,
    'England Premier League': 1.0,
    'Germany Bundesliga': 1.0,
    'Italy Serie A': 1.0,
    'France Ligue 1': 1.0,
    # Top continentals (already filter strong rosters)
    'Europe Champions League': 1.0,
    'Europa League': 0.9,
    # Other top-flight Europe
    'Liga Portugal': 0.8,
    'Eredivisie': 0.8,
    'Turkey Super Lig': 0.7,
    'Jupiler Pro League': 0.7,
    'Scotland Premiership': 0.6,
    'Russia Premier League': 0.6,
    'Austrian Bundesliga': 0.6,
    'Croatia Prva HNL': 0.55,
    'Danish Superligaen': 0.55,
    'Norwegian Eliteserien': 0.55,
    'Swedish Allsvenskan': 0.55,
    'Polish Ekstraklasa': 0.55,
    'Brack Super League': 0.55,
    'Serbian Super Liga': 0.5,
    # 2nd tier Europe
    'Bundesliga 2': 0.6,
    'England Championship': 0.6,
    'Spanish Segunda Division': 0.6,
    'Italian Serie B': 0.55,
    'French Ligue 2': 0.55,
    'Dutch Eerste Divisie': 0.5,
    'Belgian Challenger Pro League': 0.45,
    # 3rd-4th tier Europe
    'League One': 0.4,
    'League Two England': 0.3,
    # Cup-style
    'Spanish Copa Del Rey': 0.7,
    # South America (kept from prior)
    'Liga Profesional Argentina': 1.0,
    'Brasileirao': 1.0,
    'Colombia Primera A Apertura': 1.0,
    'Ecuador Liga Pro': 1.0,
    'Chile Primera': 0.5,
    'Colombia Superliga': 0.5,
    # Youth/intl tournaments
    'CONMEBOL Libertadores U20': 0.75,
    'CONMEBOL U20': 0.75,
    'CONMEBOL U17': 0.75,
    'UEFA Under 17 Championship': 0.75,
    'UEFA Under 19 Championship': 0.75,
    'UEFA Under 21 Championship': 0.8,
    'Fifa World Cup': 1.0,
    # MLS
    'MLS': 0.6,
    # Multi-comp aggregate placeholder (used when n_competitions>1)
    'MULTI': 0.7,
}
DEFAULT_LEAGUE_TIER = 0.5

YOUTH_COMPS = {'CONMEBOL Libertadores U20', 'CONMEBOL U20', 'CONMEBOL U17',
               'UEFA Under 17 Championship', 'UEFA Under 19 Championship',
               'UEFA Under 21 Championship'}

EU_COUNTRIES = {
    'Albania', 'Andorra', 'Armenia', 'Austria', 'Azerbaijan', 'Belarus',
    'Belgium', 'Bosnia-Herzegovina', 'Bulgaria', 'Croatia', 'Cyprus',
    'Czech Republic', 'Denmark', 'England', 'Estonia', 'Finland', 'France',
    'Georgia', 'Germany', 'Greece', 'Hungary', 'Iceland', 'Ireland',
    'Italy', 'Kazakhstan', 'Kosovo', 'Latvia', 'Liechtenstein', 'Lithuania',
    'Luxembourg', 'Malta', 'Moldova', 'Monaco', 'Montenegro',
    'Netherlands', 'North Macedonia', 'Norway', 'Poland', 'Portugal',
    'Romania', 'Russia', 'San Marino', 'Scotland', 'Serbia', 'Slovakia',
    'Slovenia', 'Spain', 'Sweden', 'Switzerland', 'Turkey', 'Ukraine',
    'Wales', 'Northern Ireland', 'Türkiye', 'Republic of Ireland',
    'Bosnia and Herzegovina', 'Faroe Islands', 'Gibraltar',
}

PROFILE_WEIGHTS = {
    'age': 0.35,
    'eu_nationality': 0.25,
    'league_tier': 0.20,
    'market_value': 0.20,
}

PERFORMANCE_WEIGHT = 0.60
PROFILE_WEIGHT = 0.40
MISSING_TM_PENALTY = 0.5


# ==========================================
# LOAD WEIGHTS
# ==========================================

def load_weights(path):
    """Load weights xlsx into {position: {block: [(metric, weight, invertida)]}}"""
    wb = openpyxl.load_workbook(path)
    ws = wb['Hoja1']

    weights = {}
    for row in ws.iter_rows(min_row=2, max_row=ws.max_row, values_only=True):
        pos, block, metric, weight, invertida = row
        if not pos or not metric:
            continue

        metric = METRIC_FIXES.get(metric, metric)
        inv = str(invertida).strip().lower() == 'true'

        weights.setdefault(pos, {}).setdefault(block, []).append((metric, weight, inv))

    return weights


# ==========================================
# PERFORMANCE SCORING
# ==========================================

def compute_percentile_ranks(df, weights):
    """Percentile ranks per (mapped_position, season) for all relevant metrics."""
    metrics_by_pos = {}
    for pos, blocks in weights.items():
        metrics = set()
        for block_metrics in blocks.values():
            for metric, _, _ in block_metrics:
                metrics.add(metric)
        metrics_by_pos[pos] = metrics

    for pos, metrics in metrics_by_pos.items():
        mask = df['mapped_position'] == pos
        if mask.sum() == 0:
            continue
        sub = df.loc[mask]
        for metric in metrics:
            if metric not in df.columns:
                continue
            col_name = f'_rank_{pos}_{metric}'
            # rank within (position, season): groupby Temporada then rank
            df.loc[mask, col_name] = (
                sub.groupby('Temporada')[metric]
                .rank(pct=True, na_option='keep')
            )

    return df


def compute_performance_scores(df, weights):
    """Compute performance score for each row — fully vectorized per position group."""
    all_block_names = set()
    for blocks in weights.values():
        all_block_names.update(blocks.keys())

    # Init block columns
    for block_name in all_block_names:
        df[f'block_{block_name}'] = pd.NA

    df['performance_score'] = pd.NA

    for pos, blocks in weights.items():
        mask = df['mapped_position'] == pos
        if mask.sum() == 0:
            continue

        block_scores_list = []
        for block_name, metrics in blocks.items():
            col = f'block_{block_name}'
            # Build weighted sum vectorized
            numerator = pd.Series(0.0, index=df.index[mask])
            denominator = pd.Series(0.0, index=df.index[mask])

            for metric, weight, invertida in metrics:
                rank_col = f'_rank_{pos}_{metric}'
                if rank_col not in df.columns:
                    continue
                vals = df.loc[mask, rank_col].astype(float)
                if invertida:
                    vals = 1.0 - vals
                valid = vals.notna()
                numerator += (vals * weight).fillna(0) * valid.astype(float)
                denominator += weight * valid.astype(float)

            block_score = numerator / denominator.replace(0, pd.NA)
            df.loc[mask, col] = block_score
            block_scores_list.append(block_score)

        # Performance = mean of block scores
        if block_scores_list:
            stacked = pd.concat(block_scores_list, axis=1)
            df.loc[mask, 'performance_score'] = stacked.mean(axis=1, skipna=True)

    return df


# ==========================================
# PROFILE SCORING
# ==========================================

def parse_market_value(mv_str):
    """Parse market value string like '€2.80m', '€500k' to float in millions."""
    if not mv_str or pd.isna(mv_str) or mv_str == '-':
        return None
    mv_str = str(mv_str).strip()
    match = re.search(r'[€$£]?([\d.,]+)\s*([mkMK]?)', mv_str)
    if not match:
        return None
    num = float(match.group(1).replace(',', '.'))
    unit = match.group(2).lower()
    if unit == 'm':
        return num
    elif unit == 'k':
        return num / 1000
    return num


def has_eu_nationality(nationality_str):
    """Check if player has any EU nationality."""
    if not nationality_str or pd.isna(nationality_str):
        return False
    for country in EU_COUNTRIES:
        if country.lower() in str(nationality_str).lower():
            return True
    return False


def compute_profile_scores(df):
    """Compute profile score components and combined profile score."""
    # Age factor
    def age_factor(age):
        if pd.isna(age) or age == '':
            return None
        try:
            a = int(float(age))
        except (ValueError, TypeError):
            return None
        if a < 23:
            return 1.0
        elif a <= 25:
            return 0.5
        return 0.0

    # prefer master.age, fall back to tm_age
    age_src = df.get('age')
    if age_src is None:
        age_src = df.get('tm_age', pd.Series(index=df.index, dtype=object))
    else:
        age_src = age_src.where(age_src.notna(), df.get('tm_age'))
    df['age_factor'] = age_src.apply(age_factor)
    df['_age_used'] = age_src

    # EU nationality — prefer master.nationality, fall back to tm_nationality
    nat_src = df.get('nationality')
    if nat_src is None:
        nat_src = df.get('tm_nationality', pd.Series(index=df.index, dtype=object))
    else:
        nat_src = nat_src.where(nat_src.notna() & (nat_src != ''), df.get('tm_nationality'))
    df['eu_nationality_factor'] = nat_src.apply(
        lambda x: 1.0 if has_eu_nationality(x) else 0.0
    )
    df['_nationality_used'] = nat_src

    # League tier
    df['league_tier_factor'] = df['Competencia'].map(LEAGUE_TIERS).fillna(DEFAULT_LEAGUE_TIER)

    # Market value factor
    def mv_factor(mv_str):
        mv = parse_market_value(mv_str)
        if mv is None:
            return None
        if mv < 3:
            return 1.0
        elif mv <= 5:
            return 0.5
        return -1  # sentinel for exclusion

    df['market_value_factor'] = df['tm_market_value'].apply(mv_factor)
    df['market_value_millions'] = df['tm_market_value'].apply(parse_market_value)

    # Attribute-data-missing flag — TRUE only if BOTH master and TM attrs absent
    df['tm_data_missing'] = (
        df['_age_used'].isna() | (df['_age_used'].astype(str) == '') |
        df['_nationality_used'].isna() | (df['_nationality_used'].astype(str) == '')
    )

    # Combined profile score
    def profile_score(row):
        if row['tm_data_missing']:
            numerator = (
                PROFILE_WEIGHTS['league_tier'] * row['league_tier_factor'] +
                PROFILE_WEIGHTS['eu_nationality'] * row['eu_nationality_factor']
            )
            denominator = PROFILE_WEIGHTS['league_tier'] + PROFILE_WEIGHTS['eu_nationality']
            return (numerator / denominator if denominator > 0 else 0) * MISSING_TM_PENALTY

        components = []
        w_total = 0

        if row['age_factor'] is not None:
            components.append(PROFILE_WEIGHTS['age'] * row['age_factor'])
            w_total += PROFILE_WEIGHTS['age']

        components.append(PROFILE_WEIGHTS['eu_nationality'] * row['eu_nationality_factor'])
        w_total += PROFILE_WEIGHTS['eu_nationality']

        components.append(PROFILE_WEIGHTS['league_tier'] * row['league_tier_factor'])
        w_total += PROFILE_WEIGHTS['league_tier']

        if row['market_value_factor'] is not None and row['market_value_factor'] >= 0:
            components.append(PROFILE_WEIGHTS['market_value'] * row['market_value_factor'])
            w_total += PROFILE_WEIGHTS['market_value']

        return sum(components) / w_total if w_total > 0 else 0

    df['profile_score'] = df.apply(profile_score, axis=1)

    return df


# ==========================================
# DEDUPLICATION
# ==========================================

def season_sort_key(s):
    """Convert season string to sortable int. '24-25' -> 2425, '25' -> 2500."""
    s = str(s).strip()
    if '-' in s:
        parts = s.split('-')
        return int(parts[0]) * 100 + int(parts[1])
    return int(s) * 100


def deduplicate_players(df):
    """Keep one row per player: most recent season, prefer domestic, more minutes."""
    df['_season_sort'] = df['Temporada'].apply(season_sort_key)
    df['_is_domestic'] = ~df['Competencia'].isin(YOUTH_COMPS)

    # Best performance score across all rows
    best_perf = df.groupby('playerId')['performance_score'].max().rename('best_performance_score')

    df = df.sort_values(
        ['_season_sort', '_is_domestic', 'Minutos totales'],
        ascending=[False, False, False]
    )
    deduped = df.groupby('playerId').first().reset_index()
    deduped = deduped.merge(best_perf, on='playerId', how='left')

    deduped.drop(columns=['_season_sort', '_is_domestic'], inplace=True)

    return deduped


# ==========================================
# MAIN
# ==========================================

def main():
    parser = argparse.ArgumentParser(description='Scouting Score System')
    parser.add_argument('--min-minutes', type=int, default=450)
    parser.add_argument('--output', type=str, default=str(OUTPUT_PATH))
    parser.add_argument('--season', type=str, default=None,
                        help='Comma-separated seasons to include, e.g. "25,26"')
    parser.add_argument('--max-age', type=int, default=None,
                        help='Exclude players older than this (master.age, fallback tm_age)')
    parser.add_argument('--legacy-csv', action='store_true',
                        help='Read the old per-row CSV instead of the parquet pipeline')
    args = parser.parse_args()

    print("Loading data...")
    if args.legacy_csv:
        df = pd.read_csv(CSV_PATH)
        print(f"  legacy CSV mode: {len(df)} rows × {len(df.columns)} cols")
    else:
        df = pd.read_parquet(PARQUET_PATH)
        print(f"  parquet (player-season grain): {len(df)} rows × {len(df.columns)} cols")
    weights = load_weights(WEIGHTS_PATH)

    # Map positions
    df['mapped_position'] = df['Posicion'].map(POSITION_MAP)
    unmapped = df[df['mapped_position'].isna() & (df['Posicion'] != '') & (df['Posicion'] != 'Sub')]
    if len(unmapped) > 0:
        print(f"  Warning: {len(unmapped)} rows with unmapped positions: {unmapped['Posicion'].unique()}")

    # Filter
    df = df[df['mapped_position'].notna()].copy()
    print(f"  After position filter: {len(df)} rows")

    min_mins = df['Competencia'].apply(
        lambda c: 270 if c in YOUTH_COMPS else args.min_minutes
    )
    df = df[df['Minutos totales'].astype(float) >= min_mins].copy()
    print(f"  After minutes filter (>={args.min_minutes}, >=270 youth): {len(df)} rows")

    if args.season:
        valid = {s.strip() for s in args.season.split(',')}
        df = df[df['Temporada'].astype(str).isin(valid)].copy()
        print(f"  After season filter ({args.season}): {len(df)} rows")

    # Ensure TM columns exist
    for col in ['tm_age', 'tm_nationality', 'tm_market_value', 'tm_contract_until']:
        if col not in df.columns:
            df[col] = ''

    # Position group sizes
    print("\nPosition groups:")
    for pos in sorted(df['mapped_position'].unique()):
        n = (df['mapped_position'] == pos).sum()
        print(f"  {pos}: {n} rows")

    # Performance scoring
    print("\nComputing percentile ranks...")
    df = compute_percentile_ranks(df, weights)

    print("Computing performance scores...")
    df = compute_performance_scores(df, weights)

    scored = df['performance_score'].notna().sum()
    print(f"  {scored}/{len(df)} rows scored")

    # Profile scoring
    print("\nComputing profile scores...")
    df = compute_profile_scores(df)

    # Age filter — uses _age_used (master.age fallback to tm_age)
    if args.max_age:
        age_col = pd.to_numeric(df['_age_used'], errors='coerce')
        over = age_col.notna() & (age_col > args.max_age)
        if over.sum() > 0:
            df = df[~over].copy()
            print(f"  Excluded {over.sum()} players over age {args.max_age}")

    # Market value hard filter (>5M excluded)
    pre_filter = len(df)
    df = df[df['market_value_factor'] != -1].copy()
    excluded = pre_filter - len(df)
    if excluded > 0:
        print(f"  Excluded {excluded} players with market value >5M")

    # Final scouting score
    df['scouting_score'] = (
        PERFORMANCE_WEIGHT * df['performance_score'].fillna(0) +
        PROFILE_WEIGHT * df['profile_score'].fillna(0)
    )

    # Percentile rank within (position, season)
    df['percentile_rank_in_position'] = (
        df.groupby(['mapped_position', 'Temporada'])['performance_score'].rank(pct=True)
    )

    # No dedup — output is at (player, season) grain.
    # Add best_performance_score as the player's max across seasons in this run.
    df['best_performance_score'] = df.groupby('playerId')['performance_score'].transform('max')

    result = df.sort_values('scouting_score', ascending=False).copy()
    n_unique_players = result['playerId'].nunique()
    print(f"\n  {len(result)} player-seasons across {n_unique_players} unique players")

    # Build output columns
    id_cols = ['playerId', 'Jugador', 'Equipo', 'Competencia', 'Temporada',
               'n_competitions', 'comps_list',
               'Posicion', 'mapped_position', 'Minutos totales', 'Partidos jugados']
    attr_cols = [c for c in ['age', '_age_used', 'nationality', '_nationality_used',
                              'second_nationality', 'foot', 'height', 'weight',
                              'date_of_birth', 'country_of_birth',
                              'contract_end_date', 'contract_start_date',
                              'tm_age', 'tm_nationality', 'tm_market_value',
                              'tm_contract_until', 'tm_foot', 'tm_height',
                              'tm_position', 'tm_photo']
                 if c in result.columns]
    profile_cols = ['age_factor', 'eu_nationality_factor', 'league_tier_factor',
                    'market_value_factor', 'market_value_millions', 'profile_score']
    block_cols = sorted([c for c in result.columns if c.startswith('block_')])
    score_cols = ['performance_score', 'scouting_score', 'percentile_rank_in_position',
                  'tm_data_missing', 'best_performance_score']

    out_cols = id_cols + attr_cols + profile_cols + block_cols + score_cols
    out_cols = [c for c in out_cols if c in result.columns]

    output = result[out_cols]
    output.to_csv(args.output, index=False)
    print(f"\nOutput written to: {args.output}")
    print(f"Total players: {len(output)}")

    # Summary
    print("\n" + "=" * 80)
    print("TOP 20 SCOUTING SCORES")
    print("=" * 80)
    top = output.head(20)
    for _, row in top.iterrows():
        age = row.get('_age_used', row.get('tm_age', '?'))
        mv = row.get('tm_market_value', '?')
        nat = row.get('_nationality_used', row.get('tm_nationality', '?'))
        season = row.get('Temporada', '?')
        flag = " [no attrs]" if row.get('tm_data_missing') else ""
        print(f"  {row['scouting_score']:.3f} | {str(row['Jugador'])[:25]:<25} | "
              f"{str(row['Equipo'])[:20]:<20} | {row['mapped_position']} | s:{season} | "
              f"age:{age} | {mv} | {nat}{flag}")

    # Stats
    print(f"\nScore distribution:")
    print(f"  Mean: {output['scouting_score'].mean():.3f}")
    print(f"  Median: {output['scouting_score'].median():.3f}")
    print(f"  Std: {output['scouting_score'].std():.3f}")
    print(f"  Min: {output['scouting_score'].min():.3f}")
    print(f"  Max: {output['scouting_score'].max():.3f}")

    tm_missing = output['tm_data_missing'].sum()
    print(f"\n  Players with TM data: {len(output) - tm_missing}")
    print(f"  Players missing TM data: {tm_missing}")


if __name__ == '__main__':
    main()