#!/usr/bin/env python3
"""
Essay Pre-Submission Checker for Xavier
Checks for common issues before submitting assignments
"""

import argparse
import re
from pathlib import Path

def check_essay(text: str, min_words: int = 500) -> dict:
    """Analyze essay for common issues"""
    results = {
        "word_count": 0,
        "paragraph_count": 0,
        "issues": [],
        "suggestions": [],
        "score": 100  # Start at 100, deduct for issues
    }
    
    # Basic counts
    words = text.split()
    results["word_count"] = len(words)
    
    paragraphs = [p.strip() for p in text.split('\n\n') if p.strip()]
    results["paragraph_count"] = len(paragraphs)
    
    # Check word count
    if results["word_count"] < min_words:
        deficit = min_words - results["word_count"]
        results["issues"].append(f"Word count short: {results['word_count']}/{min_words} (need {deficit} more)")
        results["suggestions"].append("Add more analysis explaining WHY your evidence matters")
        results["score"] -= 15
    
    # Check paragraph count
    if results["paragraph_count"] < 5:
        results["issues"].append(f"Only {results['paragraph_count']} paragraphs (aim for 5)")
        results["suggestions"].append("Standard essay: Introduction, 3 body paragraphs, conclusion")
        results["score"] -= 10
    
    # Check for thesis indicators in first paragraph
    if paragraphs:
        intro = paragraphs[0].lower()
        thesis_words = ["because", "therefore", "argues", "demonstrates", "shows", "proves"]
        has_thesis = any(word in intro for word in thesis_words)
        
        if not has_thesis:
            results["issues"].append("No clear thesis detected in introduction")
            results["suggestions"].append("Add a thesis: [Topic] + [Your Position] + [Reasons]")
            results["score"] -= 20
    
    # Check for informal language
    informal_patterns = [
        (r'\bI think\b', "Avoid 'I think' - state directly"),
        (r'\byou\b', "Avoid 'you' - use formal third person"),
        (r"n't\b", "Expand contractions (don't → do not)"),
        (r'\bkinda\b|\bsort of\b|\bpretty much\b', "Avoid vague language"),
        (r'\bgot\b', "Replace 'got' with more formal word (received, obtained)"),
    ]
    
    for pattern, message in informal_patterns:
        if re.search(pattern, text, re.IGNORECASE):
            results["issues"].append(f"Informal language: {message}")
            results["score"] -= 5
    
    # Check for transitions between paragraphs
    transition_words = [
        "furthermore", "additionally", "moreover", "however", "nevertheless",
        "therefore", "consequently", "in addition", "on the other hand",
        "for example", "for instance", "in conclusion", "finally"
    ]
    
    transition_count = 0
    for para in paragraphs[1:]:  # Skip intro
        first_sentence = para.split('.')[0].lower() if para else ""
        if any(word in first_sentence for word in transition_words):
            transition_count += 1
    
    if results["paragraph_count"] > 2 and transition_count < 2:
        results["issues"].append("Missing transition words between paragraphs")
        results["suggestions"].append("Start body paragraphs with: Furthermore, However, Additionally...")
        results["score"] -= 10
    
    # Check conclusion
    if paragraphs:
        conclusion = paragraphs[-1].lower()
        conclusion_words = ["in conclusion", "finally", "ultimately", "in summary", "to conclude"]
        has_conclusion = any(word in conclusion for word in conclusion_words)
        
        if not has_conclusion and results["paragraph_count"] >= 4:
            results["suggestions"].append("Consider starting conclusion with 'In conclusion' or 'Ultimately'")
    
    # Check sentence variety
    sentences = re.split(r'[.!?]+', text)
    sentences = [s.strip() for s in sentences if s.strip()]
    
    if sentences:
        lengths = [len(s.split()) for s in sentences]
        avg_length = sum(lengths) / len(lengths)
        
        if avg_length < 10:
            results["issues"].append("Sentences may be too short (average: {:.0f} words)".format(avg_length))
            results["suggestions"].append("Combine some short sentences with transitions")
            results["score"] -= 5
        elif avg_length > 25:
            results["issues"].append("Sentences may be too long (average: {:.0f} words)".format(avg_length))
            results["suggestions"].append("Break up long sentences for clarity")
            results["score"] -= 5
    
    # Ensure score doesn't go below 0
    results["score"] = max(0, results["score"])
    
    return results

def print_report(results: dict):
    """Print formatted report"""
    print("\n" + "=" * 50)
    print("📝 ESSAY CHECK REPORT")
    print("=" * 50)
    
    print(f"\n📊 Stats:")
    print(f"   Words: {results['word_count']}")
    print(f"   Paragraphs: {results['paragraph_count']}")
    print(f"   Pre-Check Score: {results['score']}/100")
    
    if results["issues"]:
        print(f"\n⚠️  Issues Found ({len(results['issues'])}):")
        for issue in results["issues"]:
            print(f"   • {issue}")
    else:
        print("\n✅ No major issues found!")
    
    if results["suggestions"]:
        print(f"\n💡 Suggestions:")
        for suggestion in results["suggestions"]:
            print(f"   • {suggestion}")
    
    print("\n" + "=" * 50)
    
    if results["score"] >= 80:
        print("🎉 Looking good! Review suggestions and submit when ready.")
    elif results["score"] >= 60:
        print("📌 A few fixes needed. Address the issues above.")
    else:
        print("🔧 Needs work. Focus on the issues before submitting.")

def main():
    parser = argparse.ArgumentParser(description="Check essay before submission")
    parser.add_argument("file", help="Path to essay file (txt or md)")
    parser.add_argument("--min-words", type=int, default=500, help="Minimum word count")
    
    args = parser.parse_args()
    
    file_path = Path(args.file)
    if not file_path.exists():
        print(f"Error: File not found: {file_path}")
        return
    
    text = file_path.read_text()
    results = check_essay(text, args.min_words)
    print_report(results)

if __name__ == "__main__":
    main()
