import pytest

pytest_plugins = ["pytest_html"]

def pytest_html_report_title(report):
    """modifying the title  of html report"""
    report.title = "Backend Testing Report"

def pytest_html_results_table_header(cells):
    cells.insert(2, "<th>Detailed trip</th>")
    cells.insert(3, "<th>Trip Duration</th>")
    cells.insert(4, "<th>Target Duration</th>")
    cells[5] = "<th>Execution time</th>"            # rename the column containing execution times to avoid confusion


def pytest_html_results_table_row(report, cells):
    trip_details = getattr(report, "trip_details", "N/A")  # Default to "N/A" if no trip data
    trip_duration = getattr(report, "trip_duration", "N/A")  # Default to "N/A" if no trip data
    target_duration = getattr(report, "target_duration", "N/A")  # Default to "N/A" if no trip data
    cells.insert(2, f"<td>{trip_details}</td>")
    cells.insert(3, f"<td>{trip_duration}</td>")
    cells.insert(4, f"<td>{target_duration}</td>")


@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    report = outcome.get_result()
    report.description = str(item.function.__doc__)

    # Attach trip_details if it exists
    if hasattr(item, "trip_details"):
        report.trip_details = " - ".join(item.trip_details)  # Convert list to string
    else:
        report.trip_details = "N/A"  # Default if trip_string is not set

    # Attach trip_duration if it exists
    if hasattr(item, "trip_duration"):
        report.trip_duration = item.trip_duration + " min"  
    else:
        report.trip_duration = "N/A"  # Default if duration is not set

    # Attach target_duration if it exists
    if hasattr(item, "target_duration"):
        report.target_duration = item.target_duration + " min"  
    else:
        report.target_duration = "N/A"  # Default if duration is not set