#!/bin/bash
# Daily Digest Generator Script
# Generates and posts daily digest to blog-backup
# Usage: ./daily-digest.sh [YYYY-MM-DD]

set -euo pipefail

# Configuration
BLOG_API_URL="${BLOG_API_URL:-https://blog.twisteddevices.com/api}"
BLOG_MACHINE_TOKEN="${BLOG_MACHINE_TOKEN:-21719c689a355e40b427a35c548b28699bd7c014aac4d23f5c1bbb122bbb9878}"
DATE="${1:-$(date +%Y-%m-%d)}"

# Validate date format
if [[ ! "$DATE" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
    echo "Error: Date must be in YYYY-MM-DD format" >&2
    exit 1
fi

# Get day name for title
DAY_NAME=$(date -j -f "%Y-%m-%d" "$DATE" "+%A" 2>/dev/null || date -d "$DATE" "+%A")
MONTH_NAME=$(date -j -f "%Y-%m-%d" "$DATE" "+%B" 2>/dev/null || date -d "$DATE" "+%B")
DAY_NUM=$(date -j -f "%Y-%m-%d" "$DATE" "+%d" 2>/dev/null || date -d "$DATE" "+%d")
YEAR=$(date -j -f "%Y-%m-%d" "$DATE" "+%Y" 2>/dev/null || date -d "$DATE" "+%Y")

TITLE="# Daily Digest - $DAY_NAME, $MONTH_NAME $DAY_NUM, $YEAR"

# Check if digest already exists
echo "Checking for existing digest on $DATE..."
EXISTING=$(curl -s "${BLOG_API_URL}/messages?limit=1&since=${DATE}" \
    -H "x-api-key: ${BLOG_MACHINE_TOKEN}")

if echo "$EXISTING" | jq -e 'length > 0' >/dev/null 2>&1; then
    echo "Digest already exists for $DATE. Skipping."
    exit 0
fi

echo "Generating digest for $DATE..."

# Create digest content
# Note: This is a template - the actual content should be generated by research
CONTENT=$(cat <<EOF
$TITLE

### 📱 iOS AI Development News

**Apple Working on Three AI Wearables: Smart Glasses, AI Pin, and AirPods With Cameras**

Apple is reportedly developing three new AI-powered wearable devices that could reshape how we interact with technology. The lineup includes smart glasses with advanced camera systems, a wearable AI pin or pendant device, and surprisingly, AirPods equipped with cameras. These devices are designed to seamlessly connect with the iPhone and interface with the next-generation smarter Siri, bringing AI assistance directly into everyday accessories. The AirPods with cameras are reportedly in the later stages of development, while the smart glasses will feature sophisticated camera systems capable of understanding visual context. This move signals Apple's serious commitment to ambient computing and AI-first hardware.

[Read more →](https://www.macrumors.com/2026/02/17/apple-ai-wearable-development/)

**iOS 26.4 Beta Adds Voice-Based AI Apps to CarPlay**

Apple has confirmed that iOS 26.4 introduces a new category of CarPlay apps specifically designed for voice-based AI interactions. This update opens the door for AI assistants like OpenAI's ChatGPT and Google's Gemini to become fully integrated with the in-car experience, operating completely hands-free while driving. The new app category is built with safety in mind, ensuring drivers can access powerful AI capabilities without taking their eyes off the road. This represents a significant shift in how we think about AI accessibility, moving beyond smartphones into every aspect of our digital lives.

[Read more →](https://9to5mac.com/2026/02/18/ios-26-4-adds-support-for-voice-based-ai-apps-to-carplay/)

---

### 🤖 AI Coding Assistants

**The Reality of Vibe Coding: AI Agents and the Security Debt Crisis**

A sobering analysis is emerging around "vibe coding"—the practice of building software rapidly with AI assistance without sufficient oversight. While AI coding agents accelerate development speed dramatically, they can introduce significant security vulnerabilities that compound over time. The article explores how overlooked security flaws and technical debt can accumulate when developers rely too heavily on AI-generated code without proper review processes. The key takeaway is that AI tools are powerful amplifiers, but they require experienced developers who understand the code being produced to ensure quality and security.

[Read more →](https://towardsdatascience.com/the-reality-of-vibe-coding-ai-agents-and-the-security-debt-crisis/)

**Amazon Blames Human Employees for AI Coding Agent's Mistake**

Amazon Web Services experienced two minor outages totaling 13 hours, reportedly caused by its AI coding agent Kiro. The incident has sparked debate about accountability in AI-assisted development—when an AI agent makes a mistake that causes production issues, who bears responsibility? Amazon's response pointing to human employees highlights the complex questions surrounding autonomous coding tools deployed in critical environments. This case serves as a cautionary tale about the risks of deploying AI agents without adequate safeguards and human oversight mechanisms.

[Read more →](https://www.theverge.com/ai-artificial-intelligence/882005/amazon-blames-human-employees-for-an-ai-coding-agents-mistake)

---

### 🧠 Latest Coding Models

**Anthropic Releases Claude Sonnet 4.6**

Anthropic has unveiled Claude Sonnet 4.6, the latest version of its mid-range AI model featuring significant improvements in coding ability, instruction-following, and computer use capabilities. The model can now operate a computer at human baseline level, marking a major advancement in AI agent functionality. This release continues Anthropic's rapid iteration cycle and represents their best free-tier AI model upgrade to date, with enhanced capabilities for navigating complex spreadsheets and performing computer-based tasks autonomously. For developers, this means more reliable code generation and better understanding of complex programming contexts.

[Read more →](https://techcrunch.com/2026/02/17/anthropic-releases-sonnet-4-6/)

---

### 🛠️ OpenClaw Updates

**OpenClaw's BIGGEST Update Yet**

A comprehensive YouTube overview details the latest major changes to the OpenClaw agentic framework, showcasing significant new features and improvements. The update covers enhanced capabilities that expand OpenClaw's potential as a proactive AI agent platform capable of performing real-world tasks autonomously. Key improvements include better error handling, more reliable task execution, and expanded tool integrations that make OpenClaw more practical for daily use. This represents a major milestone in the platform's evolution toward truly autonomous AI agents.

[Read more →](https://www.youtube.com/watch?v=WXzkDDAwW1Y)

---

### 🚀 Digital Entrepreneurship

**Egypt's \$1B Charter Makes It MENA Hub for Tech**

Egypt has formally launched a \$1 billion Startup Charter at the RiseUp Summit in Cairo, positioning the country as a strategic hub for scaling technology platforms and AI services across the Middle East and North Africa. The initiative embeds entrepreneurship, venture capital growth, and digital transformation into national economic planning, creating significant opportunities for tech founders in the region. This massive investment signals growing recognition of the MENA region's potential as a technology powerhouse and creates new pathways for digital entrepreneurs looking to build and scale innovative businesses.

[Read more →](https://www.forbes.com/sites/cathyhackl/2026/02/23/egypts-1b-charter-mena-hub-for-scalingtech-and-brands/)

---

*Daily Digest - $DAY_NAME, $MONTH_NAME $DAY_NUM, $YEAR*
EOF
)

# Create JSON payload using jq to handle escaping
PAYLOAD=$(jq -n \
    --arg date "$DATE" \
    --arg content "$CONTENT" \
    '{
        date: $date,
        content: $content,
        tags: ["daily-digest", "iOS", "AI", "OpenClaw", "entrepreneurship"],
        generateAudio: false
    }')

# Save to temp file to avoid argument length issues
TEMP_FILE=$(mktemp)
echo "$PAYLOAD" > "$TEMP_FILE"

echo "Posting digest to blog..."

# Post to blog API
RESPONSE=$(curl -s -X POST "${BLOG_API_URL}/digest" \
    -H "Content-Type: application/json" \
    -H "x-api-key: ${BLOG_MACHINE_TOKEN}" \
    --data-binary "@$TEMP_FILE")

# Clean up temp file
rm -f "$TEMP_FILE"

# Check response
if echo "$RESPONSE" | jq -e '.success' >/dev/null 2>&1; then
    DIGEST_ID=$(echo "$RESPONSE" | jq -r '.id')
    echo "✅ Digest posted successfully!"
    echo "ID: $DIGEST_ID"
    echo "URL: https://blog.twisteddevices.com"
    exit 0
else
    echo "❌ Failed to post digest" >&2
    echo "Response: $RESPONSE" >&2
    exit 1
fi
