################################################################################
# cPanel Cron Jobs Configuration
# Workflow Automation System
#
# Copy these cron jobs to your cPanel Cron Jobs interface
# or add them to your crontab via SSH: crontab -e
#
# Format: minute hour day month weekday command
# * * * * * command to execute
# │ │ │ │ │
# │ │ │ │ └─── day of week (0-7, Sunday=0 or 7)
# │ │ │ └───── month (1-12)
# │ │ └─────── day of month (1-31)
# │ └───────── hour (0-23)
# └─────────── minute (0-59)
#
# Special characters:
# * = any value
# , = value list separator
# - = range of values
# / = step values
#
# IMPORTANT: Replace ~/workflow-automation with your actual path!
################################################################################

# ══════════════════════════════════════════════════════════════════
# ESSENTIAL CRON JOBS (Recommended for all installations)
# ══════════════════════════════════════════════════════════════════

# Main Workflow - Every Hour
# Runs the main workflow to fetch, process, and publish content
0 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py >> logs/cron.log 2>&1

# Health Check - Every 6 Hours
# Monitors system health and sends alerts if issues detected
0 */6 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --check >> logs/cron_health.log 2>&1

# Database Backup - Daily at 3 AM
# Creates backup of all database files
0 3 * * * cd ~/workflow-automation && ./backup_full.sh >> logs/cron_backup.log 2>&1

# ══════════════════════════════════════════════════════════════════
# CONTENT MANAGEMENT CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Content Refresh - Daily at 2 AM
# Updates old articles with fresh content
0 2 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --refresh-old >> logs/cron_refresh.log 2>&1

# Process Content Queue - Every 30 Minutes
# Processes scheduled content from the calendar
*/30 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --queue >> logs/cron_queue.log 2>&1

# Internal Links Refresh - Weekly (Sunday at 1 AM)
# Crawls sitemap and updates internal linking database
0 1 * * 0 cd ~/workflow-automation && venv/bin/python3 workflow.py --refresh-links >> logs/cron_links.log 2>&1

# ══════════════════════════════════════════════════════════════════
# MONITORING & ANALYTICS CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Competitor Monitoring - Every 2 Hours
# Monitors competitor websites for new content
0 */2 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --monitor-competitors >> logs/cron_competitor.log 2>&1

# Real-time Competitor Monitoring - Every 30 Minutes (Optional)
# For aggressive competitor monitoring (high resource usage)
# */30 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --monitor-realtime >> logs/cron_realtime.log 2>&1

# Feedback Loop (Google Search Console) - Weekly (Monday at 4 AM)
# Analyzes GSC data and optimizes content
0 4 * * 1 cd ~/workflow-automation && venv/bin/python3 workflow.py --feedback >> logs/cron_feedback.log 2>&1

# ══════════════════════════════════════════════════════════════════
# RESEARCH & IDEATION CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Keyword Research - Weekly (Monday at 1 AM)
# Discovers new keyword opportunities
0 1 * * 1 cd ~/workflow-automation && venv/bin/python3 workflow.py --keywords >> logs/cron_keywords.log 2>&1

# Content Ideation - Daily at 5 AM
# Generates new content ideas from various sources
0 5 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --ideate >> logs/cron_ideation.log 2>&1

# Trend Analysis - Daily at 6 AM
# Analyzes trending topics and generates content ideas
0 6 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --analyze-trends >> logs/cron_trends.log 2>&1

# ══════════════════════════════════════════════════════════════════
# ADVANCED FEATURES CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Content Clustering Analysis - Weekly (Sunday at 3 AM)
# Analyzes content clusters and identifies gaps
0 3 * * 0 cd ~/workflow-automation && venv/bin/python3 workflow.py --analyze-clusters >> logs/cron_clusters.log 2>&1

# Performance Prediction - Daily at 7 AM
# Predicts performance of recent articles
0 7 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --predict-performance >> logs/cron_predict.log 2>&1

# Backlink Building - Weekly (Wednesday at 2 AM)
# Discovers backlink opportunities
0 2 * * 3 cd ~/workflow-automation && venv/bin/python3 workflow.py --build-backlinks >> logs/cron_backlinks.log 2>&1

# ══════════════════════════════════════════════════════════════════
# REPORTING CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Weekly Report - Monday at 9 AM
# Generates and sends weekly performance report
0 9 * * 1 cd ~/workflow-automation && venv/bin/python3 workflow.py --report weekly >> logs/cron_report.log 2>&1

# Monthly Report - 1st of month at 9 AM
# Generates and sends monthly performance report
0 9 1 * * cd ~/workflow-automation && venv/bin/python3 workflow.py --report monthly >> logs/cron_report.log 2>&1

# ROI Calculation - Monthly (1st at 10 AM)
# Calculates content ROI and generates report
0 10 1 * * cd ~/workflow-automation && venv/bin/python3 workflow.py --calculate-roi >> logs/cron_roi.log 2>&1

# ══════════════════════════════════════════════════════════════════
# MAINTENANCE CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Log Rotation - Daily at 5 AM
# Rotates and compresses old log files
0 5 * * * cd ~/workflow-automation && ./rotate_logs.sh >> logs/cron_rotate.log 2>&1

# Database Optimization - Weekly (Sunday at 4 AM)
# Optimizes SQLite databases for better performance
0 4 * * 0 cd ~/workflow-automation && venv/bin/python3 -c "from modules.db_utils import optimize_all_databases; optimize_all_databases()" >> logs/cron_optimize.log 2>&1

# Cleanup Old Files - Weekly (Sunday at 5 AM)
# Removes old temporary files and logs
0 5 * * 0 find ~/workflow-automation/logs -name "*.log" -mtime +30 -delete && find ~/workflow-automation/logs -name "*.log.gz" -mtime +90 -delete

# Git Pull (Auto-update) - Weekly (Sunday at 4 AM)
# Automatically updates code from repository (if using Git)
# 0 4 * * 0 cd ~/workflow-automation && git pull origin main && touch tmp/restart.txt >> logs/cron_update.log 2>&1

# ══════════════════════════════════════════════════════════════════
# SOCIAL MEDIA AUTOMATION CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Social Media Posting - Every 3 Hours (during business hours)
# Posts content to social media platforms
0 9,12,15,18 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --social-post >> logs/cron_social.log 2>&1

# Email Newsletter - Weekly (Friday at 10 AM)
# Sends weekly newsletter to subscribers
0 10 * * 5 cd ~/workflow-automation && venv/bin/python3 workflow.py --send-newsletter >> logs/cron_newsletter.log 2>&1

# ══════════════════════════════════════════════════════════════════
# QUALITY ASSURANCE CRON JOBS
# ══════════════════════════════════════════════════════════════════

# Fact Checking - Daily at 8 AM
# Runs fact-checking on recent articles
0 8 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --fact-check >> logs/cron_factcheck.log 2>&1

# Brand Voice Check - Daily at 9 AM
# Checks brand voice consistency across content
0 9 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --check-brand-voice >> logs/cron_brand.log 2>&1

# Compliance Check - Weekly (Monday at 10 AM)
# Checks content for legal compliance
0 10 * * 1 cd ~/workflow-automation && venv/bin/python3 workflow.py --check-compliance >> logs/cron_compliance.log 2>&1

# ══════════════════════════════════════════════════════════════════
# CUSTOM CRON JOBS (Examples - Customize as needed)
# ══════════════════════════════════════════════════════════════════

# Custom: High-frequency workflow (every 15 minutes)
# For sites that need very frequent updates
# */15 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py >> logs/cron.log 2>&1

# Custom: Business hours only (9 AM - 6 PM, Mon-Fri)
# 0 9-18 * * 1-5 cd ~/workflow-automation && venv/bin/python3 workflow.py >> logs/cron.log 2>&1

# Custom: Specific article processing
# 0 10 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --article-url "https://example.com/article" >> logs/cron_custom.log 2>&1

# Custom: Multi-language content generation
# 0 11 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --translate --languages en,id,ms >> logs/cron_translate.log 2>&1

# ══════════════════════════════════════════════════════════════════
# NOTIFICATION SETTINGS
# ══════════════════════════════════════════════════════════════════

# To receive email notifications for cron job output:
# Add this line at the top of your crontab:
# MAILTO=your-email@example.com

# To disable email notifications:
# MAILTO=""

# To send errors only:
# Add >/dev/null to redirect stdout, keep stderr:
# 0 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py >/dev/null

# ══════════════════════════════════════════════════════════════════
# RESOURCE OPTIMIZATION TIPS
# ══════════════════════════════════════════════════════════════════

# For shared hosting with limited resources:
# 1. Reduce frequency of resource-intensive jobs
# 2. Run jobs during off-peak hours (2-6 AM)
# 3. Avoid overlapping jobs
# 4. Use nice command to lower priority:
#    0 * * * * nice -n 19 cd ~/workflow-automation && venv/bin/python3 workflow.py

# For VPS/dedicated servers:
# 1. Can run more frequent jobs
# 2. Can enable real-time monitoring
# 3. Can run multiple jobs in parallel

# ══════════════════════════════════════════════════════════════════
# MONITORING CRON JOBS
# ══════════════════════════════════════════════════════════════════

# To check if cron jobs are running:
# crontab -l                    # List all cron jobs
# grep CRON /var/log/syslog     # Check cron execution log
# tail -f ~/workflow-automation/logs/cron.log  # Watch cron output

# To test a cron job manually:
# cd ~/workflow-automation && venv/bin/python3 workflow.py --check

# ══════════════════════════════════════════════════════════════════
# TROUBLESHOOTING
# ══════════════════════════════════════════════════════════════════

# If cron jobs are not running:
# 1. Check crontab syntax: crontab -l
# 2. Check file permissions: ls -la ~/workflow-automation/
# 3. Check Python path: which python3
# 4. Check virtual environment: source ~/workflow-automation/venv/bin/activate
# 5. Test command manually in terminal
# 6. Check cron service: service cron status (may need sudo)
# 7. Check logs: tail -f ~/workflow-automation/logs/cron.log

# Common issues:
# - Wrong path to Python or script
# - Virtual environment not activated
# - Missing execute permissions on scripts
# - Environment variables not set in cron
# - Cron service not running

# ══════════════════════════════════════════════════════════════════
# RECOMMENDED STARTER CONFIGURATION
# ══════════════════════════════════════════════════════════════════

# For new installations, start with these essential cron jobs:

# 1. Main workflow (hourly)
# 0 * * * * cd ~/workflow-automation && venv/bin/python3 workflow.py >> logs/cron.log 2>&1

# 2. Health check (every 6 hours)
# 0 */6 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --check >> logs/cron_health.log 2>&1

# 3. Daily backup (3 AM)
# 0 3 * * * cd ~/workflow-automation && ./backup_full.sh >> logs/cron_backup.log 2>&1

# 4. Content refresh (2 AM)
# 0 2 * * * cd ~/workflow-automation && venv/bin/python3 workflow.py --refresh-old >> logs/cron_refresh.log 2>&1

# 5. Weekly keyword research (Monday 1 AM)
# 0 1 * * 1 cd ~/workflow-automation && venv/bin/python3 workflow.py --keywords >> logs/cron_keywords.log 2>&1

# Then gradually add more cron jobs as needed based on your requirements.

################################################################################
# END OF CRON JOBS CONFIGURATION
################################################################################