#!/usr/bin/env python3
"""
Single source of truth for "is the TSX open today" -- weekend + full-closure
holiday check. Every cron job in this project (run_daily_scan.sh, fetch_prices.py,
check_positions.sh) calls this instead of keeping its own copy of the calendar,
so the list can't silently drift the way run_daily_scan.sh's filter thresholds
once did (see CLAUDE.md).

Deliberately does NOT skip on early-close days (e.g. Dec 24, 1pm EST close) or
settlement-only holidays where TSX stays open for trading (e.g. National Day
for Truth and Reconciliation, Sept 30 -- TMX explicitly keeps the market open
that day; only settlement is affected). Only full closures block a run.

Update HOLIDAYS each year from:
https://www.tsx.com/en/trading/calendars-and-trading-hours/calendar

CLI usage (for bash scripts): exit 0 = trading day, exit 1 = market closed.
Python usage: from is_trading_day import is_trading_day
"""
import sys
from datetime import date

HOLIDAYS = {
    # 2026 -- source: tsx.com/en/trading/calendars-and-trading-hours/calendar, confirmed 2026-07-31
    "2026-01-01",  # New Year's Day
    "2026-02-16",  # Family Day
    "2026-04-03",  # Good Friday
    "2026-05-18",  # Victoria Day
    "2026-07-01",  # Canada Day
    "2026-08-03",  # Civic Holiday
    "2026-09-07",  # Labour Day
    "2026-10-12",  # Thanksgiving Day
    "2026-12-25",  # Christmas Day
    "2026-12-28",  # Boxing Day (observed -- Dec 26 falls on a Saturday)
}


def is_trading_day(d=None):
    d = d or date.today()
    if d.weekday() >= 5:  # Saturday=5, Sunday=6
        return False
    return d.isoformat() not in HOLIDAYS


if __name__ == "__main__":
    sys.exit(0 if is_trading_day() else 1)
