import datetime
import os
import sys
import time

import typer

from celilo.common import saru_home
from celilo.reporting.PayloadTraceReport import PayloadTraceReport
from celilo.reporting.StreamReportGenerator import StreamReportGenerator
from celilo.reporting.message_transmission_report import MessageTransmissionReport
from celilo.reporting.test_controller import StreamTestController

ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))

sys.path.append(ROOT_DIR)
app = typer.Typer()

print(f"working directory: {saru_home}")


@app.command(name="stream_test", help="Runs a stream test for a specified duration.")
def stream_test(
        duration_minutes: float,
        config_path: str = typer.Option(help="Config file path"),
        app_package: str = typer.Option(
            "com.somewearlabs.atak.debug",
            help="The package name of the app to test. Options: 'com.somewearlabs.atak.debug' or 'com.atakmap.app.civ' (default)."
        ),
        workspace_id: str = typer.Option(
            help="The workspace ID to use for the test; must be a workspace that the client has already joined."
        ),
        workspace_key: str = typer.Option(
            None,
            help="The workspace-key used to join a workspace"
        ),
        workspace_name: str = typer.Option(
            None,
            help="The workspace name used to join a workspace"
        )
):
    assert duration_minutes
    assert duration_minutes
    assert config_path

    if workspace_key:
        assert workspace_name, "Workspace name must be provided if workspace key is specified"

    # config_path exists
    if not os.path.exists(config_path):
        raise Exception(f"{config_path} does not exist")

    """  
        Executes a stream test for a given duration.  

        Args:  
            duration_minutes (float): The duration of the test in minutes.  
            app_package (str, optional): The package name of the app to test. Defaults to "com.atakmap.app.civ",
            but can also use com.somewearlabs.atak.debug".  
        """

    current_time = time.time()
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_time))

    reports = StreamTestController(current_time, duration_minutes, app_package, workspace_id, config_path,
                                   workspace_key, workspace_name).start()

    payload_trace = PayloadTraceReport(current_time, duration_minutes, app_package, workspace_id, config_path)
    payload_trace.generate_report()

    return reports


@app.command(name="blank_command", help="")
def stream_test(duration_minutes: float):
    assert duration_minutes


#     for some reason, command names don't work unless there are more than 1


if __name__ == '__main__':
    app()
