{% extends 'layouts/admin.twig' %}

{% block content %}
    <ul class="breadcrumbs el">
        <li><a href="/admin/">Admin</a> /</li>
        <li><a href="{{ route('admin-subscriptions') }}">Subscriptions</a> /</li>
        <li><a href="{{ route('admin-subscription-logs') }}">Logs</a> /</li>
        <li class="end">Log #{{ log.id }}</li>
    </ul>

    <div class="content-card">
        <h1>{{ title }}</h1>

        <div class="page-actions" style="margin-bottom: 20px;">
            <a href="{{ route('admin-subscription-logs') }}" class="BUTTON">
                <i class="fa fa-arrow-left"></i> Back to Logs
            </a>
            <a href="{{ route('admin-subscription-logs-for-subscription', log.subscription_id) }}" class="BUTTON">
                <i class="fa fa-history"></i> All Logs for Subscription #{{ log.subscription_id }}
            </a>
            {% if log.subscription %}
                <a href="{{ route('admin-subscriptions-edit', log.subscription_id) }}" class="BUTTON">
                    <i class="fa fa-edit"></i> Edit Subscription
                </a>
            {% endif %}
        </div>

        {% include 'admin/messages.twig' %}
        <!-- Main Log Details -->
        <form class="style">
            <fieldset class="show">
                <h1><a class="show el">Log Details <i class="fa fa-caret-down"></i></a></h1>
                <div class="row">
                    <label>Log ID:</label>
                    <span>#{{ log.id }}</span>
                </div>
                <div class="row">
                    <label>Date/Time:</label>
                    <span>{{ log.getFormattedDate() }}</span>
                </div>
                <div class="row">
                    <label>Action Type:</label>
                    <span class="badge badge-{{ log.action_type }}">
                        {{ log.action_type|replace({'_': ' '})|title }}
                    </span>
                </div>
                <div class="row">
                    <label>Source:</label>
                    <span class="badge badge-{{ log.source|title }}">
                        {{ log.source|title }}
                    </span>
                </div>
                <div class="row">
                    <label>Description:</label>
                    <span>{{ log.action_description }}</span>
                </div>
                <div class="row">
                    <label>Actor:</label>
                    <span>{{ log.getActorName() }}</span>
                </div>
                {% if log.ip_address %}
                    <div class="row">
                        <label>IP Address:</label>
                        <span>{{ log.ip_address|e }}</span>
                    </div>
                {% endif %}
                {% if log.user_agent %}
                    <div class="row">
                        <label>User Agent:</label>
                        <div class="user-agent">{{ log.user_agent|e }}</div>
                    </div>
                {% endif %}
            </fieldset>
        </form>

        <!-- Changes Section -->
        {% if log.hasDataChanges() %}
            <form class="style">
                <fieldset class="show">
                    <h1><a class="show el">Changes Made <i class="fa fa-caret-down"></i></a></h1>
                            {% if log.old_values or log.new_values %}
                                <div class="changes-container">
                                    {% if log.old_values and log.new_values %}
                                        <!-- Show before/after comparison -->
                                        {% for field, new_value in log.new_values %}
                                            {% if log.old_values[field] is defined and log.old_values[field] != new_value %}
                                                <div class="change-item">
                                                    <div class="change-field">{{ field|replace({'_': ' '})|title }}:</div>
                                                    <div class="change-values">
                                                        <div class="old-value">
                                                            <strong>Before:</strong>
                                                            <span class="value">{{ log.old_values[field] ?? 'null' }}</span>
                                                        </div>
                                                        <div class="new-value">
                                                            <strong>After:</strong>
                                                            <span class="value">{{ new_value ?? 'null' }}</span>
                                                        </div>
                                                    </div>
                                                </div>
                                            {% endif %}
                                        {% endfor %}
                                    {% elseif log.new_values %}
                                        <!-- Show new values only (creation) -->
                                        <h5>New Values:</h5>
                                        <div class="data-display">
                                            {% for field, value in log.new_values %}
                                                <div class="data-item">
                                                    <strong>{{ field|replace({'_': ' '})|title }}:</strong> {{ value }}
                                                </div>
                                            {% endfor %}
                                        </div>
                                    {% elseif log.old_values %}
                                        <!-- Show old values only (deletion) -->
                                        <h5>Removed Values:</h5>
                                        <div class="data-display">
                                            {% for field, value in log.old_values %}
                                                <div class="data-item">
                                                    <strong>{{ field|replace({'_': ' '})|title }}:</strong> {{ value }}
                                                </div>
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                </div>
                        {% else %}
                            <p>No specific changes recorded.</p>
                        {% endif %}
                    </fieldset>
                </form>
            {% endif %}

            <!-- Metadata Section -->
            {% if log.metadata %}
                <form class="style">
                    <fieldset class="show">
                        <h1><a class="show el">Additional Information <i class="fa fa-caret-down"></i></a></h1>
                            <div class="metadata-display">
                                {% for key, value in log.metadata %}
                                    <div class="metadata-item">
                                        <strong>{{ key|replace({'_': ' '})|title }}:</strong>
                                        {% if value is iterable %}
                                            <pre>{{ value|json_encode(constant('JSON_PRETTY_PRINT')) }}</pre>
                                        {% else %}
                                            {{ value }}
                                        {% endif %}
                                    </div>
                            {% endfor %}
                        </div>
                    </fieldset>
                </form>
            {% endif %}

            <!-- Subscription Info -->
            {% if log.subscription %}
                <form class="style">
                    <fieldset class="show">
                        <h1><a class="show el">Subscription Info <i class="fa fa-caret-down"></i></a></h1>
                        <div class="row">
                            <label>Subscription ID:</label>
                            <a href="{{ route('admin-subscriptions-edit', log.subscription.id) }}">
                                #{{ log.subscription.id }}
                            </a>
                        </div>
                        <div class="row">
                            <label>Status:</label>
                            <span class="badge badge-{{ log.subscription.status }}">
                                {{ log.subscription.status|title }}
                            </span>
                        </div>
                        <div class="row">
                            <label>Member:</label>
                            <span>{{ log.subscription.member.full_name }}</span>
                        </div>
                        <div class="row">
                            <label>Created:</label>
                            <span>{{ log.subscription.created_at|date('d/m/Y H:i') }}</span>
                        </div>
                    </fieldset>
                </form>
            {% endif %}

            <!-- Member Info -->
            {% if log.member_id %}
                <form class="style">
                    <fieldset class="show">
                        <h1><a class="show el">Member Info <i class="fa fa-caret-down"></i></a></h1>
                        <div class="row">
                            <label>Name:</label>
                            <span>{{ log.member.full_name }}</span>
                        </div>
                        <div class="row">
                            <label>Email:</label>
                            <span>{{ log.member.email }}</span>
                        </div>
                        <div class="row">
                            <label>Member ID:</label>
                            <span>#{{ log.member.id }}</span>
                        </div>
                    </fieldset>
                </form>
            {% endif %}

            <!-- Recent Logs for this Subscription -->
            <form class="style">
                <fieldset class="show">
                    <h1><a class="show el">Recent Activity <i class="fa fa-caret-down"></i></a></h1>
                    {% for recent_log in recent_logs %}
                        <div class="row {% if recent_log.id == log.id %}current-log{% endif %}">
                            <label>{{ recent_log.created_at|date('d/m/Y H:i') }}:</label>
                            <div class="activity-item">
                                <span class="badge badge-{{ recent_log.action_type }}">
                                    {{ recent_log.action_type|replace({'_': ' '})|title }}
                                </span>
                                <span class="activity-description"> => {{ recent_log.action_description }}</span>
                            </div>
                        </div>
                    {% endfor %}
                    <div class="row">
                        <a href="{{ route('admin-subscription-logs-for-subscription', log.subscription_id) }}" class="BUTTON">
                            View All Logs
                        </a>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
{% endblock %}

{% block head %}
<link rel="stylesheet" href="/plugins/mtc_pharmacy_subscriptions/admin/css/style.css">
{% endblock %}