#!/usr/bin/env python3
"""
SEC Stats — Season & Sport Folder Setup Utility

Creates the recommended directory structure for one or more seasons and sports.
Run once before you start collecting files for each sport/season combination.

Usage:
  python3 setup_season.py                        # baseball + softball, current year
  python3 setup_season.py --sport baseball       # baseball only, current year
  python3 setup_season.py --sport softball 2026  # softball, specific year
  python3 setup_season.py 2026 2027              # both sports, two years
"""

import os
import sys
import argparse
from datetime import datetime

SEC_TEAMS = {
    "ALA":   "Alabama",             "ARK":   "Arkansas",
    "AUB":   "Auburn",              "FLA":   "Florida",
    "UGA":   "Georgia",             "UK":    "Kentucky",
    "LSU":   "LSU",                 "MISS":  "Ole Miss",
    "MSU":   "Mississippi State",   "MIZ":   "Missouri",
    "OKLA":  "Oklahoma",            "SC":    "South Carolina",
    "TENN":  "Tennessee",           "TEX":   "Texas",
    "TAMU":  "Texas A&M",           "VANDY": "Vanderbilt",
}

SPORTS = ["baseball", "softball"]


def setup(seasons, sports):
    base = os.path.dirname(os.path.abspath(__file__))

    for sport in sports:
        print(f"\n── {sport.upper()} ───────────────────────────────")
        for year in seasons:
            year = str(year)
            raw_base = os.path.join(base, "data", "raw", sport, year)
            arc_base = os.path.join(base, "data", "archive", sport, year)
            jso_base = os.path.join(base, "json", sport)

            created = 0
            for team_id in SEC_TEAMS:
                for path in (
                    os.path.join(raw_base, team_id),
                    os.path.join(arc_base, team_id),
                ):
                    if not os.path.exists(path):
                        os.makedirs(path)
                        created += 1

            os.makedirs(jso_base, exist_ok=True)

            print(f"  Season {year}: {created} folders created")
            print(f"    Raw:     data/raw/{sport}/{year}/TEAM_ID/")
            print(f"    Archive: data/archive/{sport}/{year}/TEAM_ID/")
            print(f"    JSON:    json/{sport}/")

    # Print team ID reference
    print("\nTeam folder names:")
    for tid, name in sorted(SEC_TEAMS.items()):
        print(f"  {tid:<6} {name}")

    print()
    print("Done. Next steps:")
    print("  1. Drop XML files into the appropriate team folder")
    print("  2. Run: ./update.sh --sport baseball 2026")
    print("     or:  ./update.sh --sport softball 2026")


def main():
    parser = argparse.ArgumentParser(
        description="Set up SEC Stats folder structure for a season"
    )
    parser.add_argument(
        "years", nargs="*",
        help="Season year(s) to set up (default: current year)"
    )
    parser.add_argument(
        "--sport", choices=SPORTS, default=None,
        help="Sport to set up (default: both baseball and softball)"
    )
    args = parser.parse_args()

    years  = args.years if args.years else [datetime.now().year]
    sports = [args.sport] if args.sport else SPORTS

    setup(years, sports)


if __name__ == "__main__":
    main()
