
    iX                        d Z ddlZddlmZ ddlmZmZmZmZ ddlZ	ddl
mZmZmZ ddlmZmZ  ej"                  d      Zej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej.                  ej.                  ej2                  ej2                  d	
Zd
ededeeeef      fdZd
ededeeeef      fdZd
edeeeef      fdZd
edeeeef      fdZy)u  
Tier 1: Official source ingestion.

Ingests roster, availability, and lineup data from the existing sportsdb tables
(PlayerInjury, SportsGame, PlayerGameMetric, etc.) which are already populated
by the SGO pipeline, CBS, ESPN, FantasyPros crons.

This module does NOT invent fake data — it reads what the existing ingestion
pipeline has already collected and normalizes it for validation.
    N)datetime)ListDictAnyOptional   )AvailabilityStatusLineupStatus
SourceTier)normalize_player_namenormalize_team_abbrevzpub-integrity)
outir
suspensiondoubtfulquestionableprobablez
day-to-daydtdactive	availableleaguerun_idreturnc                 B   | j                  t        j                  j                        }|j	                  d||f       |j                         }|j                          i }|D ]<  }t        |d         }||v r#||   }|d   |j                  dd      kD  r|||<   8|||<   > g }	|j                         D ]  \  }}|d   xs dj                         j                         }
t        j                  |
t        j                        }|j                  r|d   dk  rt        j                   }|	j#                  |d   ||j                  d	d      ||
d
|d    dt$        j&                  |j                  d      |d	        t(        j+                  dt-        |	       d|        |	S )z
    Read PlayerInjury table for confirmed availability statuses.
    Uses 2-source confirmation for OUT/IR/Suspension (matching existing logic in odds-refresh.ts).
    Single-source reports are included as QUESTIONABLE/PROBABLE.
    cursor_factorya  
        SELECT pi."playerName", pi.status, pi.source, pi.team, pi."updatedAt",
               sc.source_count
        FROM "PlayerInjury" pi
        JOIN (
            SELECT LOWER("playerName") AS pn, status, COUNT(DISTINCT source) AS source_count
            FROM "PlayerInjury"
            WHERE LOWER(league) = LOWER(%s)
              AND "updatedAt" > NOW() - INTERVAL '7 days'
            GROUP BY LOWER("playerName"), status
        ) sc ON LOWER(pi."playerName") = sc.pn AND pi.status = sc.status
        WHERE LOWER(pi.league) = LOWER(%s)
          AND pi."updatedAt" > NOW() - INTERVAL '7 days'
        ORDER BY pi."updatedAt" DESC
    
playerNamesource_countr   status    teamzPlayerInjury (z	 sources)	updatedAt)	player_namenormalized_namer"   availability_statusdetailed_statussourcesource_tiersource_timestampr   [official] Ingested z availability reports for )cursorpsycopg2extrasRealDictCursorexecutefetchallcloser   getitemslowerstripINJURY_STATUS_MAPr	   UNKNOWNis_blockingQUESTIONABLEappendr   OFFICIALloginfolen)connr   r   currowsplayer_reportsrowpnameexistingresults
raw_statusavails               O/var/www/html/rainmaker/backend/scripts/publishing_integrity/official_ingest.py!ingest_availability_from_injuriesrK   &   s    ++X__%C%C+
DC KK  &	  <<>DIIK 13N $%c,&78N"%e,H>"X\\.!%DD(+u% #u$ G$**, 
s(m)r00288:
!%%j2D2L2LM ^!4q!8&33E|,$GGFB'#()&s>':&;9E%.. # 4

 
	( HH#CL>1KF8TUN    c                    | j                  t        j                  j                        }|j	                  d|f       |j                         }|j                          g }|D ]L  }|j                  |d   t        |d         t        |d   xs d      |dt        j                  |d   |d       N t        j                  d	t        |       d
|        |S )z
    Read PlayerGameMetric to determine current roster/team assignments.
    PGM tracks which players appear in games for which teams.
    r   a  
        SELECT DISTINCT ON (LOWER("playerName"))
            "playerName", team, "gameDate", league
        FROM "PlayerGameMetric"
        WHERE LOWER(league) = LOWER(%s)
          AND "gameDate" > NOW() - INTERVAL '30 days'
        ORDER BY LOWER("playerName"), "gameDate" DESC
    r   r"   r    PlayerGameMetricgameDate)r$   r%   r"   r   r(   r)   last_game_dater   r+   z roster entries from PGM for )r,   r-   r.   r/   r0   r1   r2   r;   r   r   r   r<   r=   r>   r?   )r@   r   r   rA   rB   rG   rD   s          rJ   ingest_rosters_from_pgmrQ   g   s    
 ++X__%C%C+
DCKK   <<>DIIKG 
|,4S5FG)#f+*;<(%..!*o	
 		
 HH#CL>1NvhWXNrL   c                 @   | j                  t        j                  j                        }|j	                  d|f       |j                         }|j                          t        j                  dt        |       d|        |D cg c]  }t        |       c}S c c}w )z7Read today's games from rm_events for the given league.r   aa  
        SELECT event_id, league, home_team, away_team, home_short, away_short,
               starts_at, status
        FROM rm_events
        WHERE LOWER(league) = LOWER(%s)
          AND DATE(starts_at AT TIME ZONE 'America/New_York') = (NOW() AT TIME ZONE 'America/New_York')::date
          AND status != 'ended'
        ORDER BY starts_at ASC
    [official] Found z today's games for )r,   r-   r.   r/   r0   r1   r2   r=   r>   r?   dict)r@   r   rA   rB   rs        rJ   ingest_today_gamesrV      s    
++X__%C%C+
DCKK   <<>DIIKHH T+>vhGH!"DG"""s   Bc                 Z   | j                  t        j                  j                        }|j	                  d|f       |j                         }|j                          g }|D ]  }|d   xs i }|j                  dg       }|D ]t  }|j                  dd      }	|	s|j                  |d   ||d   |d	   |	t        |	      |j                  d
d      |j                  dd      |j                  dd      d	       v  t        j                  dt        |       dt        |       d|        |S )u   
    Read player prop highlights from today's forecasts in rm_forecast_cache.
    These are the props that would be published — the validation targets.
    r   av  
        SELECT fc.event_id, fc.league, fc.home_team, fc.away_team,
               fc.forecast_data, fc.confidence_score
        FROM rm_forecast_cache fc
        WHERE LOWER(fc.league) = LOWER(%s)
          AND (fc.starts_at AT TIME ZONE 'America/New_York')::date
              = (NOW() AT TIME ZONE 'America/New_York')::date
          AND fc.forecast_data IS NOT NULL
    forecast_dataprop_highlightsplayerr    event_id	home_team	away_teamproprecommendation	reasoning)	r[   r   r\   r]   r$   r%   	prop_typer_   r`   rS   z player props across z forecasts for )r,   r-   r.   r/   r0   r1   r2   r3   r;   r   r=   r>   r?   )
r@   r   rA   rB   propsrD   fdrY   r^   r$   s
             rJ   ingest_forecast_propsrd      s8   
 ++X__%C%C+
DCKK   <<>DIIKE !'R&&!2B7# 	D((8R0KLL
O  - -*#8#E!XXfb1"&((+;R"@!XXk26
 
		& HH U,A#d)O\b[cdeLrL   ) __doc__loggingr   typingr   r   r   r   psycopg2.extrasr-   enumsr	   r
   r   normalizationr   r   	getLoggerr=   OUTINACTIVE	SUSPENDEDDOUBTFULr:   PROBABLE	AVAILABLEr7   strrK   rQ   rV   rd    rL   rJ   <module>rt      s-  	   , ,  ? ? Gg( !!

%
%$.."++&33"++$11** **#-- >C > >dSVX[S[nI] >B!# !s !tDcN?S !H#S #T$sCx.-A #(( (T#s(^0D (rL   