<!--
# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

-->
{% set block = results.block %}
{% set tool = results.tool %}
{% set timestamp = results.timestamp %}
{% set stages = results.stages %}
{% set coverage = results.coverage %}
{% set failed_jobs = results.failed_jobs %}
<div class="container-md">
    <div class="row">
        <div class="col">
            {% if breadcrumbs %}
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb">
                    {% for url, label in breadcrumbs[:-1] %}
                        <li class="breadcrumb-item"><a href="{{ url }}">{{ label }}</a></li>
                    {% endfor %}
                    <li class="breadcrumb-item active" aria-current="page">
                        {{ breadcrumbs[-1][1] if breadcrumbs[-1] is iterable and breadcrumbs[-1]|length == 2 else breadcrumbs[-1] }}
                    </li>
                </ol>
            </nav>
            {% endif %}
        </div>
    </div>

    <div class="row py-3">
        <div class="col">
            <h2>Simulation Results: {{ block.name }}</h2>
        </div>
    </div>

    <div class="row">
        <div class="col-4 col-md-3">
            <div class="text-center mb-2">&nbsp;</div>
            <span class="badge text-bg-secondary">
                {{ timestamp.strftime("%d/%m/%Y %H:%M:%S") }}
            </span>
            <a class="badge text-bg-secondary link-underline link-underline-opacity-0"
               href="{{ block.url }}">
                sha: {{ block.commit[:7] }}
            </a>
            <a class="badge text-bg-secondary link-underline link-underline-opacity-0"
                        href="{{ block.name }}.json">json</a>
            <span class="badge text-bg-secondary">Branch: {{ block.branch }}</span>
            <span class="badge text-bg-secondary">
                Tool: {{ tool.name }} [{{ tool.version }}]
            </span>
        </div>

{% macro coverage_stat(cov, kind, label) %}
    {% if cov and cov|attr(kind) is not none %}
        {% set value = cov|attr(kind) %}
        <div class="col">
            <ul class="list-group list-group-horizontal">
                <li class="list-group-item list-group-item-secondary px-2 py-1">
                    {{ label }}
                </li>
                <li class="list-group-item py-1">{{ "%.2f"|format(value) }} %</li>
            </ul>
        </div>
    {% endif %}
{% endmacro %}


        {% if coverage %}
        <div class="col-4 col-md-6">
            <div class="text-center mb-2">Coverage statistics</div>
            <div class="container small">
                <div class="row g-0 row-cols-1 row-cols-md-2 row-cols-lg-3 row-cols-xl-4">
                    {{ coverage_stat(coverage, "average", "Total") }}

                    {% set code = coverage.code %}
                    {{ coverage_stat(code, "average", "code") }}
                    {{ coverage_stat(coverage, "assertion", "assert") }}
                    {{ coverage_stat(coverage, "functional", "func") }}

                    {{ coverage_stat(code, "block", "block") }}
                    {{ coverage_stat(code, "line_statement", "line") }}
                    {{ coverage_stat(code, "branch", "branch") }}
                    {{ coverage_stat(code, "condition_expression", "cond") }}
                    {{ coverage_stat(code, "toggle", "toggle") }}
                    {{ coverage_stat(code, "fsm", "FSM") }}
                </div>
            </div>
        </div>
        {% endif %}

        <div class="col-4 col-md-3">
            <div class="text-center mb-2">Validation stages</div>
            <div class="card">
                <table class="table table-sm table-borderless table-hover m-0 small">
                    <tbody>
                        {% for s_name, stage in stages.items() %}
                        <tr>
                            <td class="c{{ (stage.percent / 10)|int }}">
                                {{ s_name }}
                            </td>
                            <td class="w-100">
                                <div class="progress" role="progressbar"
                                     aria-valuenow="{{ (stage.percent / 3)|int }}"
                                     aria-valuemin="0" aria-valuemax="100">
                                    <div class="progress-bar"
                                         style="width: {{ stage.percent }}%"></div>
                                </div>
                            </td>
                            <td>{{ "%.2f" | format(stage.percent) }}%</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="row py-3">
        <div class="col">
            <div class="accordion accordion-flush" id="accordionStages">
                {% for s_name, stage in stages.items() %}
                <div class="accordion-item">
                    <div class="accordion-header">
                        <button class="accordion-button w-100" type="button"
                                data-bs-toggle="collapse"
                                data-bs-target="#collapse{{ loop.index }}"
                                aria-expanded="true"
                                aria-controls="collapse{{ loop.index }}">
                            <div class="w-100 text-start">{{ s_name }}</div>
                            <div class="w-100 text-center">
                                {{ stage.passed }}/{{ stage.total }} passed
                            </div>
                            <div class="w-100 text-end me-3">
                                {{ "%.2f" | format(stage.percent) }} %
                            </div>
                        </button>
                    </div>
                    <div id="collapse{{ loop.index }}"
                         class="accordion-collapse collapse show"
                         data-bs-parent="#accordionStages">
                        <div class="accordion-body table-responsive p-0">
                            <table class="table table-sm table-hover">
                                <thead>
                                    <tr>
                                        <th>Testpoint</th>
                                        <th>Test</th>
                                        <th>Max Runtime</th>
                                        <th>Sim Time</th>
                                        <th>Pass</th>
                                        <th>Total</th>
                                        <th>%</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for tp_name, tp in stage.testpoints.items() %}
                                    <tr class="table-secondary">
                                        <td colspan="4">{{ tp_name }}</td>
                                        <td>{{ tp.passed }}</td>
                                        <td>{{ tp.total }}</td>
                                        <td>{{ "%.2f" | format(tp.percent) }}</td>
                                    </tr>
                                    {% for t_name, t in tp.tests.items() %}
                                    <tr>
                                        <td></td>
                                        <td>{{ t_name }}</td>
                                        <td>{{ "%.3f" | format(t.max_time) }}s</td>
                                        <td>{{ "%.3f" | format(t.sim_time) }}us</td>
                                        <td>{{ t.passed }}</td>
                                        <td>{{ t.total }}</td>
                                        <td class="c{{ (t.percent / 10)|int }}">
                                            {{ "%.2f" | format(t.percent) }}
                                        </td>
                                    </tr>
                                    {% endfor %}
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        </div>
    </div>

    <div class="row py-3">
        <div class="col">
            <h3>Error Messages</h3>
            <table class="table table-sm table-hover">
                <thead>
                    <tr>
                        <th>&nbsp;&nbsp;</th>
                        <th>Test</th>
                        <th>seed</th>
                        <th>line</th>
                        <th>log context</th>
                    </tr>
                </thead>
                <tbody>
                {% for msg, job_list in failed_jobs.buckets.items() %}
                    <tr class="table-primary">
                        <td colspan=5>{{ msg }}</td>
                    </tr>
                    {% for job in job_list %}
                    <tr>
                        <th scope="row"></th>
                        <td>{{ job.name }}</td>
                        <td>{{ job.seed }}</td>
                        <td>{{ job.line }}</td>
                        <td>
                        {% for line in job.log_context %}
                            <div>{{ line }}</div>
                        {% endfor %}
                        </td>
                    </tr>
                    {% endfor %}
                {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
