{% extends "frontend/base.html" %}
{% load i18n static %}
{% block html_title %}
    {% translate "Dashboard" %} for {{ object.name }}
{% endblock html_title %}
{% block page_title %}{% endblock %}
{% block content %}
    <section class="section">
        <div class="row">
            <div class="col-12">
                <div class="card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start">
                            <div>
                                <h4 class="mb-2">{% translate "Welcome to" %} {{ object.name }}</h4>
                                <p class="text-muted mb-0">
                                    {% if has_instances %}
                                        {% translate "Here's an overview of your organization's services and resources." %}
                                    {% else %}
                                        {% translate "Ready to get started? Discover services and create your first instance." %}
                                    {% endif %}
                                </p>
                            </div>
                            {% if user_role %}
                                <div class="text-end">
                                    <span class="badge bg-light text-dark">
                                        {% if user_role == "owner" %}
                                            <i class="bi bi-star-fill me-1"></i>{% translate "Owner" %}
                                        {% elif user_role == "admin" %}
                                            <i class="bi bi-gear me-1"></i>{% translate "Administrator" %}
                                        {% else %}
                                            <i class="bi bi-person me-1"></i>{% translate "Member" %}
                                        {% endif %}
                                    </span>
                                </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
            </div>
        </div>
        {% if has_instances %}
            <div class="row">
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="stats-icon purple me-3">
                                    <i class="bi bi-hdd-stack"></i>
                                </div>
                                <div>
                                    <h6 class="text-muted font-semibold mb-0">{% translate "Service Instances" %}</h6>
                                    <h4 class="font-extrabold mb-0 mt-1">{{ service_instances_count }}</h4>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-body">
                            <div class="d-flex align-items-center">
                                <div class="stats-icon blue me-3">
                                    <i class="bi bi-people"></i>
                                </div>
                                <div>
                                    <h6 class="text-muted font-semibold mb-0">{% translate "Team Members" %}</h6>
                                    <h4 class="font-extrabold mb-0 mt-1">{{ members_count }}</h4>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        {% endif %}
        <div class="row">
            {% if has_instances %}
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">{% translate "Recent Service Instances" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>{% translate "Name" %}</th>
                                            <th>{% translate "Service" %}</th>
                                            <th>{% translate "Created" %}</th>
                                            <th>{% translate "Actions" %}</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for instance in service_instances %}
                                            <tr>
                                                <td>
                                                    <a href="{{ instance.urls.base }}"
                                                       class="fw-semibold text-decoration-none">{{ instance.display_name }}</a>
                                                </td>
                                                <td>
                                                    <div class="d-flex align-items-center">
                                                        {% if instance.context.service_offering.service.logo %}
                                                            <img src="{{ instance.context.service_offering.service.logo.url }}"
                                                                 alt="{{ instance.context.service_offering.service.name }}"
                                                                 class="me-2"
                                                                 style="width: 20px;
                                                                        height: 20px;
                                                                        object-fit: contain">
                                                        {% endif %}
                                                        <span class="small text-muted">{{ instance.context.service_offering.service.name }}</span>
                                                    </div>
                                                </td>
                                                <td>
                                                    <span class="small text-muted">{{ instance.created_at|date:"M d, Y" }}</span>
                                                </td>
                                                <td>
                                                    <div class="btn-group btn-group-sm" role="group">
                                                        <a href="{{ instance.urls.base }}"
                                                           class="btn btn-outline-primary btn-sm"
                                                           title="{% translate 'View Details' %}">
                                                            <i class="bi bi-eye"></i>
                                                        </a>
                                                        {% if instance.has_change_permission %}
                                                            <a href="{{ instance.urls.update }}"
                                                               class="btn btn-outline-secondary btn-sm"
                                                               title="{% translate 'Edit' %}">
                                                                <i class="bi bi-pencil"></i>
                                                            </a>
                                                        {% endif %}
                                                    </div>
                                                </td>
                                            </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <div class="text-center mt-3">
                                <a href="{% url 'frontend:organization.instances' organization=object.slug %}"
                                   class="btn btn-outline-primary me-2">{% translate "View All Instances" %}</a>
                                <a href="{% url 'frontend:organization.services' organization=object.slug %}"
                                   class="btn btn-primary">{% translate "Create New Instance" %}</a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">{% translate "Quick Actions" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="list-group list-group-flush quick-actions">
                                <a href="{{ object.urls.services }}"
                                   class="list-group-item list-group-item-action d-flex align-items-center">
                                    <i class="bi h2 bi-grid-3x3-gap me-3 text-primary"></i>
                                    <div>
                                        <h6 class="mb-1">{% translate "Browse Services" %}</h6>
                                        <small class="text-muted">{% translate "Explore available services" %}</small>
                                    </div>
                                </a>
                                <a href="{{ object.urls.instances }}"
                                   class="list-group-item list-group-item-action d-flex align-items-center">
                                    <i class="bi h2 bi-server me-3 text-success"></i>
                                    <div>
                                        <h6 class="mb-1">{% translate "Manage Instances" %}</h6>
                                        <small class="text-muted">{% translate "View and configure instances" %}</small>
                                    </div>
                                </a>
                                <a href="{{ object.urls.details }}"
                                   class="list-group-item list-group-item-action d-flex align-items-center">
                                    <i class="bi h2 bi-gear me-3 text-warning"></i>
                                    <div>
                                        <h6 class="mb-1">{% translate "Organization Settings" %}</h6>
                                        <small class="text-muted">{% translate "Configure organization details" %}</small>
                                    </div>
                                </a>
                                <a href="{{ organization.urls.support }}"
                                   class="list-group-item list-group-item-action d-flex align-items-center">
                                    <i class="bi h2 bi-life-preserver me-3 text-info"></i>
                                    <div>
                                        <h6 class="mb-1">{% translate "Get Support" %}</h6>
                                        <small class="text-muted">{% translate "Access help and documentation" %}</small>
                                    </div>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            {% else %}
                {# if has_instances #}
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">{% translate "Get Started" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="text-center py-4">
                                <div class="mb-4">
                                    <i class="bi bi-rocket fs-1 text-primary"></i>
                                </div>
                                <h5 class="mb-3">{% translate "Ready to deploy your first service?" %}</h5>
                                <p class="text-muted mb-4">
                                    {% translate "Browse our service catalog and deploy databases, storage, and other services with just a few clicks." %}
                                </p>
                                <a href="{% url 'frontend:organization.services' organization=object.slug %}"
                                   class="btn btn-primary btn-lg">
                                    <i class="bi bi-search me-2"></i>
                                    {% translate "Discover Services" %}
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">{% translate "Next Steps" %}</h4>
                        </div>
                        <div class="card-body">
                            <div class="timeline">
                                <div class="timeline-item">
                                    <div class="timeline-marker bg-success"></div>
                                    <div class="timeline-content">
                                        <h6 class="text-success">{% translate "Organization Created" %}</h6>
                                        <small class="text-muted">{% translate "You're all set up!" %}</small>
                                    </div>
                                </div>
                                <div class="timeline-item">
                                    <div class="timeline-marker bg-primary"></div>
                                    <div class="timeline-content">
                                        <h6>{% translate "Discover Services" %}</h6>
                                        <small class="text-muted">{% translate "Browse what's available" %}</small>
                                    </div>
                                </div>
                                <div class="timeline-item">
                                    <div class="timeline-marker"></div>
                                    <div class="timeline-content">
                                        <h6 class="text-muted">{% translate "Deploy Your First Service" %}</h6>
                                        <small class="text-muted">{% translate "Create an instance" %}</small>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            {% endif %}
        </div>
    </section>
{% endblock content %}
