"""
Custom Flask CLI commands.
"""

import click
from flask.cli import with_appcontext
from app.services.work_hours_service import calculate_and_store_real_work_hours
from app import db # Import db if direct session manipulation is needed, or rely on app context
from flask import current_app

@click.command("process-real-hours")
@with_appcontext
def process_real_hours_command():
    """
    Manually triggers the calculation and storage of real work hours.
    This processes WorkEvents and updates the UserRealWorkSummary table.
    Useful for initial data backfilling, testing, or ad-hoc runs.
    """
    current_app.logger.info("Manual trigger for process-real-hours command received.")
    try:
        calculate_and_store_real_work_hours(current_app)
        current_app.logger.info("process-real-hours command finished successfully.")
    except Exception as e:
        current_app.logger.error(f"Error during process-real-hours command: {e}", exc_info=True)
        click.echo(f"An error occurred: {e}")

def register_cli_commands(app):
    """
    Registers custom CLI commands with the Flask application.
    """
    app.cli.add_command(process_real_hours_command)
    app.logger.info("Registered custom CLI commands.") 