{% extends "frontend/base.html" %}
{% load i18n static pprint_filters %}
{% block html_title %}
    {% block page_title %}
        {{ instance.name }}
    {% endblock page_title %}
{% endblock html_title %}
{% block page_title_extra %}
    <div>
        {% if has_change_permission %}
            <a href="{{ instance.urls.update }}" class="btn btn-primary me-1 mb-1">{% translate "Edit" %}</a>
        {% endif %}
        {% if has_delete_permission %}
            <button type="button"
                    class="btn btn-danger me-1 mb-1"
                    hx-get="{{ instance.urls.delete }}"
                    hx-target="#deleteInstanceModalContent"
                    data-bs-toggle="modal"
                    data-bs-target="#deleteInstanceModal">{% translate "Delete" %}</button>
        {% endif %}
    </div>
{% endblock page_title_extra %}
{% block content %}
    <section class="section">
        <div class="row match-height mb-5">
            <div class="col-12 col-md-5">
                <div class="card">
                    <div class="card-header">
                        <h4>{% translate "Details" %}</h4>
                    </div>
                    <div class="card-body">
                        <dl class="row">
                            <dt class="col-sm-4">{% translate "Service" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.context.service_definition.service.name }}
                            </dd>
                            <dt class="col-sm-4">{% translate "Service Provider" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.context.service_offering.provider.name }}
                            </dd>
                            <dt class="col-sm-4">{% translate "Control Plane" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.context.control_plane.name }}
                            </dd>
                            <dt class="col-sm-4">{% translate "Created By" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.created_by|default:"-" }}
                            </dd>
                            <dt class="col-sm-4">{% translate "Created At" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.created_at|date:"SHORT_DATETIME_FORMAT" }}
                            </dd>
                            <dt class="col-sm-4">{% translate "Updated At" %}</dt>
                            <dd class="col-sm-8">
                                {{ instance.updated_at|date:"SHORT_DATETIME_FORMAT" }}
                            </dd>
                        </dl>
                    </div>
                </div>
            </div>
            <div class="col-12 col-md-7">
                {% if instance.status_conditions %}
                    <div class="card">
                        <div class="card-header">
                            <h4>{% translate "Status" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="table-responsive">
                                    <table class="table table-bordered">
                                        <thead>
                                            <tr>
                                                <th>{% translate "Type" %}</th>
                                                <th>{% translate "Status" %}</th>
                                                <th>{% translate "Last Transition Time" %}</th>
                                                <th>{% translate "Reason" %}</th>
                                                <th>{% translate "Message" %}</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% for condition in instance.status_conditions %}
                                                <tr>
                                                    <td>{{ condition.type }}</td>
                                                    <td>
                                                        {% if condition.status == "True" %}
                                                            <span class="badge text-bg-success">True</span>
                                                        {% elif condition.status == "False" %}
                                                            <span class="badge text-bg-danger">False</span>
                                                        {% else %}
                                                            <span class="badge text-bg-secondary">{{ condition.status }}</span>
                                                        {% endif %}
                                                    </td>
                                                    <td>{{ condition.lastTransitionTime|date:"SHORT_DATETIME_FORMAT" }}</td>
                                                    <td>{{ condition.reason|default:"-" }}</td>
                                                    <td>{{ condition.message|truncatewords:20|default:"-" }}</td>
                                                </tr>
                                            {% endfor %}
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                {% endif %}
                {% include "includes/control_plane_user_info.html" with control_plane=instance.context.control_plane %}
            </div>
            {% if instance.spec and spec_fieldsets %}
                <div class="col-12">
                    <div class="card">
                        <div class="card-header">
                            <h4>{% translate "Specification" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-12">
                                    <!-- Tabs -->
                                    <ul class="nav nav-tabs" role="tablist">
                                        {% for fieldset in spec_fieldsets %}
                                            <li class="nav-item" role="presentation">
                                                <a href="#spec-tab-{{ forloop.counter }}"
                                                   class="nav-link {% if forloop.first %}active{% endif %}"
                                                   data-bs-toggle="tab"
                                                   role="tab">{{ fieldset.title }}</a>
                                            </li>
                                        {% empty %}
                                            <li class="nav-item ms-2">{% translate "No specification details available." %}</li>
                                        {% endfor %}
                                    </ul>
                                    <!-- Tab Content -->
                                    <div class="tab-content pt-3">
                                        {% for fieldset in spec_fieldsets %}
                                            <div class="tab-pane fade {% if forloop.first %}show active{% endif %}"
                                                 id="spec-tab-{{ forloop.counter }}"
                                                 role="tabpanel">
                                                <!-- Main Fields -->
                                                <dl class="row">
                                                    {% for field in fieldset.fields %}
                                                        <dt class="col-sm-3">{{ field.label }}</dt>
                                                        <dd class="col-sm-9">
                                                            {% if field.value|default:""|stringformat:"s"|slice:":1" == "{" or field.value|default:""|stringformat:"s"|slice:":1" == "[" %}
                                                                <pre>{{ field.value|pprint }}</pre>
                                                            {% else %}
                                                                {{ field.value|default:"-" }}
                                                            {% endif %}
                                                        </dd>
                                                    {% endfor %}
                                                </dl>
                                                <!-- Nested Fieldsets -->
                                                {% for sub_key, sub_fieldset in fieldset.fieldsets.items %}
                                                    <h5>{{ sub_fieldset.title }}</h5>
                                                    <dl class="row">
                                                        {% for field in sub_fieldset.fields %}
                                                            <dt class="col-sm-3">{{ field.label }}</dt>
                                                            <dd class="col-sm-9">
                                                                {% if field.value|default:""|stringformat:"s"|slice:":1" == "{" or field.value|default:""|stringformat:"s"|slice:":1" == "[" %}
                                                                    <pre>{{ field.value|pprint }}</pre>
                                                                {% else %}
                                                                    {{ field.value|default:"-" }}
                                                                {% endif %}
                                                            </dd>
                                                        {% endfor %}
                                                    </dl>
                                                {% endfor %}
                                            </div>
                                        {% empty %}
                                            <p>{% translate "No specification details to display." %}</p>
                                        {% endfor %}
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endif %}
            {% if instance.connection_credentials %}
                <div class="card">
                    <div class="card-header">
                        <h4>{% translate "Connection Credentials" %}</h4>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>{% translate "Name" %}</th>
                                        <th>{% translate "Value" %}</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for key, value in instance.connection_credentials.items %}
                                        <tr>
                                            <td>{{ key }}</td>
                                            <td>
                                                {% if key == "error" %}
                                                    <span class="text-danger">{{ value }}</span>
                                                {% else %}
                                                    <code>{{ value }}</code>
                                                {% endif %}
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>
    </section>
    <!-- Delete Confirmation Modal -->
    <div class="modal fade"
         id="deleteInstanceModal"
         tabindex="-1"
         aria-labelledby="deleteInstanceModalLabel"
         aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content" id="deleteInstanceModalContent">
                {# Content will be loaded here by HTMX #}
                <div class="modal-header">
                    <h5 class="modal-title" id="deleteInstanceModalLabel">{% translate "Confirm Deletion" %}</h5>
                    <button type="button"
                            class="btn-close"
                            data-bs-dismiss="modal"
                            aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="d-flex justify-content-center">
                        <div class="spinner-border" role="status">
                            <span class="visually-hidden">{% translate "Loading..." %}</span>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% translate "Cancel" %}</button>
                </div>
            </div>
        </div>
    </div>
{% endblock content %}
