{% extends "package/read_base.html" %}
{% import 'macros/form.html' as form %}

{% block primary_content_inner %}
<h2 class="hide-heading">{{ _('Fuseki') }}</h2>
<div id="ajax-status" data-module="fuseki">
    <form class="add-to-group" method="post">
        {{ h.csrf_input() }}
        <div class="col-12 d-flex align-items-center justify-content-between">
            <div class="d-flex align-items-center">
                <button class="col-auto me-2 btn btn-secondary {% if not
                    service_status %}disabled{% endif %}" name="create/update" type="submit">
                    <i class="fa fa-refresh"></i> {{ _('Create/Update') }}
                </button>
                <button class="col-auto me-2 btn btn-danger invisible" name="delete" type="submit">
                    <i class="fa fa-trash"></i> {{ _('Delete') }}
                </button>
                <a class="col-auto mr-2 btn btn-primary mb-0 invisible" name="query"
                    href="{{ h.fuseki_query_url(pkg_dict)|safe }}" role="button">
                    <i class="fa fa-play"></i> {{ _('Query Dataset') }}
                </a>
            </div>
            <!-- Status Indicator Section -->
            <style>
                .indicator {
                    width: 20px;
                    height: 20px;
                    border-radius: 50%;
                    background-color: red;
                    /* Default: Service unavailable */
                }
            </style>
            <div class="d-flex align-items-center">
                <div id="service-indicator" class="indicator" data-bs-toggle="tooltip"
                    title="{{ _('The status of the service (Green means available, Red means unavailable)') }}" {% if
                    service_status %} style="background-color: green;" {% endif %}>
                </div>
            </div>
        </div>
        
        <!-- Informational Section -->
        <div class="alert alert-info mt-3" role="alert">
            <h5 class="alert-heading"><i class="fa fa-info-circle"></i> {{ _('Apache Jena Fuseki Access Information') }}</h5>
            <hr>
            <ol class="mb-0">
                <li class="mb-2">
                    <strong>{{ _('Secured Access:') }}</strong> 
                    {% if pkg_dict.private %}
                    {{ _('This is a <strong>private dataset</strong>. SPARQL endpoints are only accessible to authorized CKAN users with read permission on this dataset.') }}
                    {% else %}
                    {{ _('This is a <strong>public dataset</strong>. SPARQL endpoints are accessible to anyone without authentication.') }}
                    {% endif %}
                </li>
                <li class="mb-2">
                    <strong>{{ _('Authentication Methods:') }}</strong>
                    <ul class="mb-0">
                        <li>{{ _('<strong>Browser:</strong> Log in to CKAN and access endpoints through your browser session') }}</li>
                        <li>{{ _('<strong>API/curl:</strong> Use CKAN API token in Authorization header') }} 
                            <code style="font-size: 0.85em;">Authorization: YOUR_API_TOKEN</code>
                        </li>
                    </ul>
                </li>
                <li class="mb-2">
                    <strong>{{ _('SPARQL Endpoint:') }}</strong> 
                    <code style="font-size: 0.85em;">{{ h.fuseki_sparql_url(pkg_dict) }}</code>
                </li>
                <li class="mb-2">
                    <strong>{{ _('Named Graphs:') }}</strong> 
                    {{ _('Each resource file is uploaded to its own named graph within a dedicated dataset for this CKAN package.') }}
                </li>
            </ol>
        </div>
        <hr class="">
        {% if status.metadata is defined %}
        {% set reasoner = status.metadata.reasoner %}
        {% set reasoning = status.metadata.reasoning %}
        {% set persistant = status.metadata.persistant %}
        {% set reasoner = status.metadata.reasoner %}
        {% if not reasoning %}
        {% set disable_res = true %}
        {% endif %}
        {% else %}
        {% set reasoning = false %}
        {% set persistant = false %}
        {% set disable_res = true %}
        {% set reasoner = 'http://jena.hpl.hp.com/2003/OWLFBRuleReasoner' %}
        {% endif %}
        <div class="row m-0 gx-3 gy-2 align-items-center">
            <div class="col-auto">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name='persistent' id="check-persistent" 
                        data-bs-toggle="tooltip" 
                        title="{{ _('Store data persistently on disk. Persistent datasets survive server restarts and are included dcat catalog for cross-dataset queries.') }}"
                        {% if persistant%}checked{% endif %}>
                    <label class="form-check-label" for="check-persistent">persistent</label>
                </div>

            </div>
            <div class="col-auto">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name='reasoning' id="check-reasoning" 
                        data-bs-toggle="tooltip" 
                        title="{{ _('Enable OWL/RDFS reasoning to infer additional triples based on ontology definitions. This computes implicit knowledge from explicit statements.') }}"
                        {% if reasoning%}checked{% endif %}>
                    <label class="form-check-label" for="check-reasoning">reasoning</label>
                </div>
            </div>
            <div class="col-auto">
                <label class="visually-hidden" for="reasoner">Reasoner</label>
                <select class="form-select" id="reasoner" name='reasoner' {% if disable_res%}disabled{% endif %}>
                    {% for item in reasoners %}
                    <option value="{{ item.value }}" {% if item.value==reasoner %}selected{% endif %}>
                        {{
                        item.name }}
                    </option>
                    {% endfor %}
                </select>
            </div>

            <!-- <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" name='persistent' id="ckeck-persistent" {% if
                    status.metadata is defined %} {% if status.metadata.persistent%}checked='true' {% endif %} {% endif
                    %}>
                <label class="form-check-label" for="ckeck-persistent">persistent</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="checkbox" name='reasoning' id="ckeck-reasoning" {% if
                    status.metadata is defined %} {% if status.metadata.reasoning%}checked='true' {% endif %} {% endif
                    %}>
                <label class="form-check-label" for="ckeck-reasoning">reasoning</label>
            </div>
            <div class="input-group-prepend">
                <label class="input-group-text" for="reasoner">Reasoner</label>
            </div>
            <select class="form-select" id="reasoner" name='reasoner'>
                {% for item in reasoners %}
                <option value="{{ item.value }}" {% if item.name=="fullOWL" %}selected{% endif %}>{{ item.name }}
                </option>
                {% endfor %}
            </select>
 -->
        </div>
        <hr class="">

        <table class="table table-bordered table-sm m-0">
            <thead class="">
                <tr>
                    <th>{{ _('upload') }}</th>
                    <th>{{ _('name') }}</th>
                    <th>{{ _('format') }}</th>
                    <th>{{ _('mime type') }}</th>
                    <th>{{ _('size') }} [B]</th>
                </tr>
            </thead>
            <tbody>
                {% for resource in pkg_dict.resources %}
                <tr>
                    <td>
                        <div class="form-check form-switch">
                            <input class="form-check-input" type="checkbox" name='resid' id="ckeck-{{resource.id}}"
                                value={{resource.id}} {% if status.metadata is defined %} {% if resource.id in
                                status.metadata.res_ids%}checked='true' {% endif %} {% elif
                                h.fuseki_show_tools(resource) %}checked='true' {% endif %}>
                        </div>
                    </td>
                    <td>{{resource.name}}</td>
                    <td>{{resource.format}}</td>
                    <td>{{resource.mimetype}}</td>
                    <td>{{resource.size}}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </form>
    <hr class="mtb-0">
    <div class="invisible" name=status>
        <table class="table table-bordered">
            <colgroup>
                <col width="150">
                <col>
            </colgroup>
            <tr>
                <th>{{ _('Status') }}</th>
                <td>{{status.status}}</td>
            </tr>
            <tr>
                <th>{{ _('Last updated') }}</th>
                {% if status.status %}
                <td><span class="date" title="{{ h.render_datetime(status.last_updated, with_hours=True) }}">{{
                        h.time_ago_from_timestamp(status.last_updated) }}</span></td>
                {% else %}
                <td>{{ _('Never') }}</td>
                {% endif %}
            </tr>
        </table>

        <h3>{{ _('Graph Update Log') }}</h3>
        <ul class="activity" name="log">
        </ul>
    </div>
</div>
{% endblock %}

{% block scripts %}
{{ super()}}
{% asset 'fuseki/fuseki' %}
{% endblock %}