#!/usr/bin/env python3
"""
Example usage of the queue system.
"""

import sys
import time
from pathlib import Path

# Add project root directory to Python path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))

from task_queue.manager import queue_manager
from task_queue.tasks import process_file_async, process_multiple_files_async


def example_single_file():
    """Example: Process a single file."""
    print("=== Example: Process a single file ===")

    project_id = "test_project"
    file_path = "public/test_document.txt"

    # Enqueue file for processing
    task_id = queue_manager.enqueue_file(
        project_id=project_id,
        file_path=file_path,
        original_filename="example_document.txt"
    )

    print(f"Task submitted, task ID: {task_id}")

    # Check task status
    time.sleep(2)
    status = queue_manager.get_task_status(task_id)
    print(f"Task status: {status}")


def example_multiple_files():
    """Example: Batch process files."""
    print("\n=== Example: Batch process files ===")

    project_id = "test_project_batch"
    file_paths = [
        "public/test_document.txt",
        "public/goods.xlsx"  # Assuming this file exists
    ]
    original_filenames = [
        "batch_document_1.txt",
        "batch_goods.xlsx"
    ]

    # Enqueue multiple files for processing
    task_ids = queue_manager.enqueue_multiple_files(
        project_id=project_id,
        file_paths=file_paths,
        original_filenames=original_filenames
    )

    print(f"Batch tasks submitted, task IDs: {task_ids}")


def example_zip_file():
    """Example: Process a zip file."""
    print("\n=== Example: Process a zip file ===")

    project_id = "test_project_zip"
    zip_path = "public/all_hp_product_spec_book2506.zip"

    # Enqueue zip file for processing
    task_id = queue_manager.enqueue_zip_file(
        project_id=project_id,
        zip_path=zip_path
    )

    print(f"Zip task submitted, task ID: {task_id}")


def example_queue_stats():
    """Example: Get queue statistics."""
    print("\n=== Example: Queue statistics ===")

    stats = queue_manager.get_queue_stats()
    print("Queue statistics:")
    for key, value in stats.items():
        if key != "recent_tasks":
            print(f"  {key}: {value}")


def example_cleanup():
    """Example: Cleanup tasks."""
    print("\n=== Example: Cleanup tasks ===")

    project_id = "test_project"

    # Enqueue cleanup task (delayed 10 seconds)
    task_id = queue_manager.enqueue_cleanup_task(
        project_id=project_id,
        older_than_days=1,  # Clean files older than 1 day
        delay=10
    )

    print(f"Cleanup task submitted, task ID: {task_id}")


def main():
    """Main entry point."""
    print("Queue System Usage Examples")
    print("=" * 50)

    try:
        # Run examples
        example_single_file()
        example_multiple_files()
        example_zip_file()
        example_queue_stats()
        example_cleanup()

        print("\n" + "=" * 50)
        print("Examples completed!")
        print("\nTo check task execution, run:")
        print("python queue/consumer.py --check")
        print("\nTo start the queue consumer, run:")
        print("python queue/consumer.py")

    except Exception as e:
        print(f"Error running examples: {str(e)}")


if __name__ == "__main__":
    main()
