#!/usr/bin/env python3
# quote - quote of the day (built-in list, deterministic by date)
# Usage: quote [--date YYYY-MM-DD] [--random] [--list] [--number N]
#   With no options, prints today's quote — the same quote all day, every year
#   on the same date. --random picks any quote; --number picks one by index.
import argparse, random, sys
from datetime import date

QUOTES = [
    ("The best way to predict the future is to invent it.", "Alan Kay"),
    ("Simplicity is the soul of efficiency.", "Austin Freeman"),
    ("First, solve the problem. Then, write the code.", "John Johnson"),
    ("The only way to do great work is to love what you do.", "Steve Jobs"),
    ("It is not the strongest of the species that survives, but the most adaptable.", "Charles Darwin"),
    ("Everything should be made as simple as possible, but not simpler.", "Albert Einstein"),
    ("The best time to plant a tree was twenty years ago. The second best time is now.", "Chinese proverb"),
    ("What gets measured gets managed.", "Peter Drucker"),
    ("Stay hungry, stay foolish.", "Stewart Brand"),
    ("Whether you think you can or you think you can't — you're right.", "Henry Ford"),
    ("The secret of getting ahead is getting started.", "Mark Twain"),
    ("Programs must be written for people to read, and only incidentally for machines to execute.", "Harold Abelson"),
    ("Premature optimization is the root of all evil.", "Donald Knuth"),
    ("The most dangerous phrase in the language is, 'We've always done it this way.'", "Grace Hopper"),
    ("It does not matter how slowly you go as long as you do not stop.", "Confucius"),
    ("Give me six hours to chop down a tree and I will spend the first four sharpening the axe.", "Abraham Lincoln"),
    ("The function of good software is to make the complex appear to be simple.", "Grady Booch"),
    ("In theory, theory and practice are the same. In practice, they are not.", "Yogi Berra"),
    ("A journey of a thousand miles begins with a single step.", "Laozi"),
    ("The best error message is the one that never shows up.", "Thomas Fuchs"),
    ("Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away.", "Antoine de Saint-Exupéry"),
    ("Do not wait to strike till the iron is hot, but make it hot by striking.", "William Butler Yeats"),
    ("The only true wisdom is in knowing you know nothing.", "Socrates"),
    ("Well done is better than well said.", "Benjamin Franklin"),
    ("Code is like humor. When you have to explain it, it's bad.", "Cory House"),
    ("The future depends on what you do today.", "Mahatma Gandhi"),
    ("Talk is cheap. Show me the code.", "Linus Torvalds"),
    ("A bug is never just a mistake. It represents something bigger: an error of thinking.", "Edsger Dijkstra"),
    ("The best way out is always through.", "Robert Frost"),
    ("Luck is what happens when preparation meets opportunity.", "Seneca"),
    ("Make it work, make it right, make it fast.", "Kent Beck"),
]

def pick_for(d):
    return QUOTES[d.timetuple().tm_yday % len(QUOTES)]

def selftest():
    q1 = pick_for(date(2026, 12, 3))
    q2 = pick_for(date(2026, 12, 3))
    assert q1 == q2  # deterministic by date
    assert pick_for(date(2025, 12, 3)) == q1  # same day-of-year, any year
    assert pick_for(date(2026, 12, 4)) != q1 or len(QUOTES) == 1
    assert all(len(q) == 2 and q[0] and q[1] for q in QUOTES)
    print(f"quote selftest: ok ({len(QUOTES)} quotes, deterministic by date)")
    return 0

def main():
    p = argparse.ArgumentParser(prog="quote",
        description="Quote of the day (built-in list, deterministic by date).")
    p.add_argument("--date", help="quote for this date YYYY-MM-DD (default today)")
    p.add_argument("--random", action="store_true", help="pick a random quote")
    p.add_argument("--list", action="store_true", help="list all quotes with numbers")
    p.add_argument("--number", type=int, help="print quote N (1-based)")
    p.add_argument("--selftest", action="store_true", help="run built-in checks and exit")
    a = p.parse_args()
    if a.selftest:
        return selftest()
    if a.list:
        for i, (q, who) in enumerate(QUOTES, 1):
            print(f"{i:>2}. \"{q}\" — {who}")
        return 0
    if a.number is not None:
        if not 1 <= a.number <= len(QUOTES):
            print(f"quote: --number must be 1-{len(QUOTES)}", file=sys.stderr); return 2
        q, who = QUOTES[a.number - 1]
    elif a.random:
        q, who = random.SystemRandom().choice(QUOTES)
    else:
        try:
            d = date.fromisoformat(a.date) if a.date else date.today()
        except ValueError as e:
            print(f"quote: bad date: {e}", file=sys.stderr); return 2
        q, who = pick_for(d)
    print(f"\"{q}\"")
    print(f"  — {who}")
    return 0

if __name__ == "__main__":
    sys.exit(main())
