"""
Smart Farm Photo Keyword Tagging AI - Main Processing Script
"""

import os
import sys
import pandas as pd
from datetime import datetime
import argparse

# Add src to path for imports
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from src.data.image_processor import ImageProcessor
from src.model.keyword_generator import AgricultureKeywordGenerator

def process_agricultural_photos(input_dir: str = "data/raw", output_dir: str = "outputs"):
    """Main function to process agricultural photos and generate keywords"""

    print("🚜 Smart Farm Photo Keyword Tagging AI")
    print("=" * 50)

    # Initialize components
    print("Initializing image processor...")
    image_processor = ImageProcessor(input_dir)

    print("Initializing AI keyword generator...")
    keyword_generator = AgricultureKeywordGenerator()

    # Process images
    print(f"\nProcessing images from: {input_dir}")
    image_df = image_processor.batch_process_images(input_dir)

    if image_df.empty:
        print("No images found to process!")
        return

    print(f"Found {len(image_df)} images to process")

    # Generate keywords for each image
    results = []
    for idx, row in image_df.iterrows():
        if 'error' in row:
            print(f"Skipping {row['filename']} due to error: {row['error']}")
            continue

        print(f"Processing {row['filename']}...")

        try:
            # Generate keywords and title
            ai_results = keyword_generator.generate_keywords(row['filepath'])

            # Create result row
            result = {
                'filename': row['filename'],
                'human_keywords': '',  # Placeholder for human keywords
                'ai_keywords': ', '.join(ai_results['keywords']),
                'ai_title': ai_results['title'],
                'location': row.get('location', ''),
                'caption': ai_results['caption']
            }

            results.append(result)
            print(f"  ✓ Generated {len(ai_results['keywords'])} keywords")

        except Exception as e:
            print(f"  ✗ Error processing {row['filename']}: {e}")
            continue

    # Create output DataFrame
    results_df = pd.DataFrame(results)

    # Save to CSV
    os.makedirs(output_dir, exist_ok=True)
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_file = os.path.join(output_dir, f"agricultural_keywords_{timestamp}.csv")

    results_df.to_csv(output_file, index=False)

    print(f"\n✅ Processing complete!")
    print(f"Results saved to: {output_file}")
    print(f"Processed {len(results_df)} images successfully")

    # Display sample results
    print("\n📊 Sample Results:")
    print("-" * 80)
    for idx, row in results_df.head(3).iterrows():
        print(f"File: {row['filename']}")
        print(f"Title: {row['ai_title']}")
        print(f"Keywords: {row['ai_keywords']}")
        print(f"Location: {row['location'] if row['location'] else 'Not available'}")
        print("-" * 80)

    return output_file

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process agricultural photos for keyword tagging')
    parser.add_argument('--input', '-i', default='data/raw', help='Input directory with images')
    parser.add_argument('--output', '-o', default='outputs', help='Output directory for results')

    args = parser.parse_args()

    try:
        output_file = process_agricultural_photos(args.input, args.output)
        print(f"\n🎉 Success! Check your results in: {output_file}")
    except Exception as e:
        print(f"\n❌ Error: {e}")
        sys.exit(1)