{% extends "wagtailadmin/base.html" %}
{% load i18n %}

{% block titletag %}{% trans "Mobile App" %}{% endblock %}

{% block content %}
    <header class="w-header">
        <div class="row">
            <div class="left">
                <div class="col">
                    <h1 class="w-header__title">{% trans "Mobile App" %}</h1>
                </div>
            </div>
        </div>
    </header>

    <div class="nice-padding">
        <form method="get">
            <label for="site_id">{% trans "Site" %}</label>
            <select id="site_id" name="site_id" onchange="this.form.submit()">
                {% for site_option in sites %}
                    <option value="{{ site_option.pk }}"{% if site_option.pk == selected_site.pk %} selected{% endif %}>
                        {{ site_option.hostname }}
                    </option>
                {% endfor %}
            </select>
        </form>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Status" %}</h2>
            <ul>
                {% for check in checks %}
                    <li><strong>{{ check.level|upper }}</strong> {{ check.code }}: {{ check.message }}</li>
                {% endfor %}
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Configuration" %}</h2>
            <ul>
                <li>{% trans "Manifest" %}: <code>{{ manifest_path }}</code></li>
                <li>{% trans "Service worker" %}: <code>{{ service_worker_path }}</code></li>
                <li>{% trans "Offline fallback" %}: <code>{{ offline_path }}</code></li>
                <li>{% trans "Generated app path" %}: <code>{{ generated_path }}</code></li>
                <li>{% trans "App ID" %}: <code>{{ settings_obj.app_id|default:"-" }}</code></li>
                <li>{% trans "App version" %}: <code>{{ app_version }}</code></li>
                <li>{% trans "Build number" %}: <code>{{ build_number }}</code></li>
                <li>{% trans "Display mode" %}: <code>{{ settings_obj.display_mode }}</code></li>
                <li>{% trans "Orientation" %}: <code>{{ settings_obj.orientation }}</code></li>
                <li>{% trans "Bridge tag" %}: <code>{{ bridge_tag }}</code></li>
                <li>{% trans "Bridge script required" %}: {% if bridge_script_required %}{% trans "Yes" %}{% else %}{% trans "No" %}{% endif %}</li>
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Branding asset readiness" %}</h2>
            <ul>
                <li>{% if branding_icon_present %}✓{% else %}⚠{% endif %} {% trans "App icon present" %}</li>
                <li>{% if branding_splash_present %}✓{% else %}⚠{% endif %} {% trans "Splash image present" %}</li>
                <li>{% if adaptive_icon_generated %}✓{% else %}⚠{% endif %} {% trans "Adaptive icon generated" %}</li>
                <li>{% if branding_manifest_present %}✓{% else %}⚠{% endif %} {% trans "Branding manifest generated" %}</li>
            </ul>
            <ul>
                {% for asset in branding_assets %}
                    <li>{% if asset.exists %}✓{% else %}⚠{% endif %} <code>{{ asset.filename }}</code></li>
                {% endfor %}
            </ul>
            {% if not branding_icon_present %}
                <p>{% trans "Warning: a high-resolution square icon is required before the branding asset package can be fully generated." %}</p>
            {% endif %}
            {% if branding_icon_present and not branding_splash_present %}
                <p>{% trans "Warning: splash image is missing. A fallback splash can be generated, but a dedicated splash asset is recommended." %}</p>
            {% endif %}
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Native capability planning" %}</h2>
            <ul>
                {% for capability in capabilities %}
                    <li>
                        <strong>{{ capability.display_name }}</strong>:
                        {% if capability.enabled %}{% trans "Enabled" %}{% else %}{% trans "Disabled" %}{% endif %}
                        <br>
                        <span>{{ capability.description }}</span>
                        <br>
                        <small>{% trans "Key" %}: <code>{{ capability.key }}</code></small>
                        <br>
                        <small>{% trans "Status" %}: <code>{{ capability.implementation_status }}</code></small>
                        <br>
                        <small>{% trans "Platforms" %}: <code>{{ capability.supported_platforms|join:", " }}</code></small>
                        <br>
                        <small>{% trans "Runtime verification required" %}: {% if capability.runtime_verification_required %}{% trans "Yes" %}{% else %}{% trans "No" %}{% endif %}</small>
                        {% if capability.required_capacitor_packages %}
                            <br>
                            <small>{% trans "Required Capacitor packages" %}: <code>{{ capability.required_capacitor_packages|join:", " }}</code></small>
                        {% endif %}
                    </li>
                {% endfor %}
            </ul>
            <p><strong>{% trans "Enabled capabilities" %}</strong>:
                {% if enabled_capabilities %}
                    {% for capability in enabled_capabilities %}<code>{{ capability.key }}</code>{% if not forloop.last %}, {% endif %}{% endfor %}
                {% else %}
                    {% trans "None" %}
                {% endif %}
            </p>
            <p><strong>{% trans "Planned capabilities" %}</strong>:
                {% if planned_capabilities %}
                    {% for capability in planned_capabilities %}<code>{{ capability.key }}</code>{% if not forloop.last %}, {% endif %}{% endfor %}
                {% else %}
                    {% trans "None" %}
                {% endif %}
            </p>
            <p><strong>{% trans "Runtime verification backlog" %}</strong>:
                {% if runtime_verification_capabilities %}
                    {% for capability in runtime_verification_capabilities %}<code>{{ capability.key }}</code>{% if not forloop.last %}, {% endif %}{% endfor %}
                {% else %}
                    {% trans "None" %}
                {% endif %}
            </p>
            <ul>
                {% for note in capability_dashboard_notes %}
                    <li>{{ note }}</li>
                {% endfor %}
            </ul>
            {% if native_upload_dependencies %}
                <p>{% trans "Generated native upload dependencies" %}: <code>{{ native_upload_dependencies|join:", " }}</code></p>
            {% endif %}
            {% if bridge_dependencies %}
                <p>{% trans "Generated bridge dependencies" %}: <code>{{ bridge_dependencies|join:", " }}</code></p>
            {% endif %}
            {% if settings_obj.enable_secure_storage %}
                <p>{% trans "Secure storage backend" %}: <code>{{ secure_storage_backend }}</code></p>
            {% endif %}
            {% if settings_obj.enable_camera_uploads and not settings_obj.ios_camera_usage_description %}
                <p>{% trans "Warning: camera uploads are enabled but the iOS camera usage description is still empty." %}</p>
            {% endif %}
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Permission placeholders" %}</h2>
            <h3>{% trans "Android" %}</h3>
            <ul>
                {% for item in permission_placeholders.android %}
                    <li><strong>{{ item.title }}</strong>: {{ item.placeholder }}</li>
                {% endfor %}
            </ul>
            <h3>{% trans "iOS" %}</h3>
            <ul>
                {% for item in permission_placeholders.ios %}
                    <li><strong>{{ item.title }}</strong>: {{ item.placeholder }}</li>
                {% endfor %}
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Deep link planning" %}</h2>
            <ul>
                {% for note in deep_link_notes %}
                    <li>{{ note }}</li>
                {% endfor %}
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Runtime policies" %}</h2>
            <ul>
                <li>{% trans "Same-origin links" %}: <code>{{ runtime_policies.external_links.same_origin }}</code></li>
                <li>{% trans "Cross-origin links" %}: <code>{{ runtime_policies.external_links.cross_origin }}</code></li>
                <li>{% trans "Allowed external schemes" %}: <code>{{ runtime_policies.external_links.allowed_schemes|join:", " }}</code></li>
                <li>{% trans "Blocked schemes" %}: <code>{{ runtime_policies.external_links.blocked_schemes|join:", " }}</code></li>
                <li>{% trans "Download handling" %}: <code>{{ runtime_policies.downloads.mode }}</code></li>
                <li>{% trans "Native download manager" %}: {% if runtime_policies.downloads.native_manager %}{% trans "Yes" %}{% else %}{% trans "No" %}{% endif %}</li>
                <li>{% trans "Diagnostics global" %}: <code>{{ runtime_policies.diagnostics_global }}</code></li>
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "PWA / offline readiness" %}</h2>
            <ul>
                <li>{% trans "Generated manifest" %}: <code>{{ generated_pwa.manifest_path }}</code></li>
                <li>{% trans "Generated service worker" %}: <code>{{ generated_pwa.service_worker_path }}</code></li>
                <li>{% trans "Generated offline page" %}: <code>{{ generated_pwa.offline_html_path }}</code></li>
                <li>{% trans "Generated cache version" %}: <code>{{ generated_pwa.cache_version }}</code></li>
                <li>{% trans "Service worker registration" %}: {% if generated_pwa.service_worker_registration %}{% trans "Enabled" %}{% else %}{% trans "Disabled" %}{% endif %}</li>
                <li>{% trans "Standalone-ready display mode" %}: {% if generated_pwa.standalone_ready %}{% trans "Yes" %}{% else %}{% trans "No" %}{% endif %}</li>
            </ul>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Suggested commands" %}</h2>
            <pre>python manage.py mobileapp_check --site-id={{ selected_site.pk }}
python manage.py mobileapp_generate --site-id={{ selected_site.pk }}
python manage.py mobileapp_build_android --site-id={{ selected_site.pk }}
python manage.py mobileapp_build_ios --site-id={{ selected_site.pk }}</pre>
        </section>

        <section style="margin-top: 1.5rem;">
            <h2>{% trans "Store readiness warning" %}</h2>
            <p>{{ store_warning }}</p>
            {% if recommended_capacitor_packages %}
                <p>{% trans "If you implement the enabled native features, start with these packages:" %}</p>
                <p><code>{{ recommended_capacitor_packages|join:" " }}</code></p>
            {% endif %}
        </section>
    </div>
{% endblock %}
