#!/usr/bin/env python3
"""
fortune - Random wisdom and fortune

Combines wisdom collection with classic fortune vibes.
"""

import json
import random
from datetime import datetime
from pathlib import Path

WORKSPACE = Path("/home/wdjones/.openclaw/workspace")
WISDOM_FILE = WORKSPACE / "data" / "wisdom.json"

# Built-in fortunes if wisdom is empty
FORTUNES = [
    "The best time to start was yesterday. The second best time is now.",
    "Simple is better than complex.",
    "Done is better than perfect.",
    "You are not your code.",
    "First, solve the problem. Then, write the code.",
    "Code is read more often than it is written.",
    "The only way to go fast is to go well.",
    "Make it work, make it right, make it fast.",
    "Weeks of coding can save you hours of planning.",
    "There are only two hard things in CS: cache invalidation and naming things.",
    "It works on my machine!",
    "The quieter you become, the more you can hear.",
    "What you do today is important because you're exchanging a day of your life for it.",
    "The obstacle is the way.",
    "We suffer more in imagination than in reality.",
    "Focus on what you can control.",
    "The impediment to action advances action. What stands in the way becomes the way.",
    "You have power over your mind, not outside events.",
    "Think of yourself as dead. Now take what's left and live it properly.",
    "If it is not right, do not do it. If it is not true, do not say it.",
]

def load_wisdom() -> list:
    """Load wisdom entries."""
    if WISDOM_FILE.exists():
        try:
            with open(WISDOM_FILE) as f:
                data = json.load(f)
                return [w['text'] for w in data]
        except:
            pass
    return []

def fortune():
    """Get a random fortune."""
    # Combine wisdom and built-in fortunes
    wisdom = load_wisdom()
    all_fortunes = FORTUNES + wisdom
    
    selected = random.choice(all_fortunes)
    
    width = min(60, max(len(selected), 40))
    
    print()
    print("╭" + "─" * (width + 2) + "╮")
    
    # Word wrap
    words = selected.split()
    lines = []
    current = ""
    for word in words:
        if len(current) + len(word) + 1 <= width:
            current = current + " " + word if current else word
        else:
            lines.append(current)
            current = word
    if current:
        lines.append(current)
    
    for line in lines:
        print(f"│ {line:<{width}} │")
    
    print("╰" + "─" * (width + 2) + "╯")
    print()

def daily_fortune():
    """Get today's fortune (consistent for the day)."""
    wisdom = load_wisdom()
    all_fortunes = FORTUNES + wisdom
    
    # Seed with today's date
    day_seed = int(datetime.now().strftime("%Y%m%d"))
    random.seed(day_seed)
    selected = random.choice(all_fortunes)
    random.seed()
    
    print("\n🌅 Today's Fortune:")
    
    width = min(60, max(len(selected), 40))
    print("─" * (width + 4))
    
    words = selected.split()
    lines = []
    current = ""
    for word in words:
        if len(current) + len(word) + 1 <= width:
            current = current + " " + word if current else word
        else:
            lines.append(current)
            current = word
    if current:
        lines.append(current)
    
    for line in lines:
        print(f"  {line}")
    
    print("─" * (width + 4))
    print()

def main():
    import sys
    
    if len(sys.argv) > 1 and sys.argv[1] == 'daily':
        daily_fortune()
    else:
        fortune()

if __name__ == "__main__":
    main()
