# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

import click
from rich.console import Console

from llama_verifications.functional_tests.generate_report import create_tests_report


@click.command(name="generate-tests-report")
@click.option(
    "--provider",
    type=str,
    multiple=True,
    default=None,
    help="Provider(s) to include/test. Default: meta_reference, together, fireworks, openai",
)
@click.option(
    "--output-file",
    type=click.Path(dir_okay=False, writable=True, path_type=str),
    default=None,
    help="Output file for the report (defaults to TESTS_REPORT.md).",
)
@click.option(
    "--test-filter",
    type=str,
    default=None,
    help="Keyword expression to filter tests (like pytest -k, only used with --run-tests)",
)
@click.option(
    "--run-tests",
    is_flag=True,
    default=False,
    help="Run tests before generating the report. If not set, uses existing results.",
)
def generate_tests_report_command(provider, output_file, test_filter, run_tests):
    """Run verification tests and/or generate a markdown report."""
    providers_to_run = []
    if provider:
        for p_item in provider:
            providers_to_run.extend(item.strip() for item in p_item.split(","))
        providers_to_run = [p for p in providers_to_run if p]
    providers_arg = providers_to_run if providers_to_run else None

    console = Console()

    console.print("Starting report generation...")
    console.print(f"  Run tests: {run_tests}")
    if providers_arg:
        console.print(f"  Providers specified: {providers_arg}")
    else:
        console.print("  Providers: Not specified (will use defaults if running tests, or discover if not)")
    if run_tests and test_filter:
        console.print(f"  Test filter: {test_filter}")
    if output_file:
        console.print(f"  Output file: {output_file}")
    else:
        console.print("  Output file: Default (TESTS_REPORT.md)")

    create_tests_report(
        providers=providers_arg,
        keyword=test_filter,
        output_file=output_file,
        run_tests_flag=run_tests,
    )
    console.print("[bold green]Report command finished successfully.[/]")
