{% extends 'base.html' %}

{% block title %}Dashboard - Matzeflix{% endblock %}

{% block head %}
<!-- Google Cast SDK -->
<script type="text/javascript" src="https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1"></script>
<style>
    .media-card {
        transition: all 0.3s ease;
        border: none;
        background: #1f1f1f;
        overflow: hidden;
    }
    .media-card:hover {
        transform: scale(1.05);
        z-index: 10;
        box-shadow: 0 10px 20px rgba(0,0,0,0.5);
    }
    .media-img {
        aspect-ratio: 2/3;
        object-fit: cover;
    }
    .status-badge {
        font-size: 0.7rem;
        padding: 0.3em 0.6em;
    }
    .cast-button {
        --connected-color: #e50914;
        --disconnected-color: #ffffff;
    }
</style>
{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h2 class="fw-bold mb-0">Dashboard</h2>
        <p class="text-muted">Welcome to your Matzeflix control center.</p>
    </div>
    <div class="d-flex gap-2">
        <google-cast-launcher class="cast-button"></google-cast-launcher>
        <button class="btn btn-outline-light btn-sm" onclick="location.reload()">
            <i class="bi bi-arrow-clockwise"></i>
        </button>
    </div>
</div>

<!-- Service Status Row -->
<div class="row g-4 mb-5">
    {% for service, data in service_status.items %}
    <div class="col-md-6 col-lg-3">
        <div class="card h-100 border-0 shadow-sm">
            <div class="card-body p-4">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="p-2 rounded bg-dark">
                        {% if service == 'jellyfin' %}
                            <i class="bi bi-play-btn fs-4 text-primary"></i>
                        {% elif service == 'radarr' %}
                            <i class="bi bi-film fs-4 text-info"></i>
                        {% elif service == 'sonarr' %}
                            <i class="bi bi-tv fs-4 text-success"></i>
                        {% elif service == 'jellyseerr' %}
                            <i class="bi bi-plus-circle fs-4 text-warning"></i>
                        {% endif %}
                    </div>
                    {% if data.status == 'Online' %}
                        <span class="badge bg-success status-badge">ONLINE</span>
                    {% elif data.status == 'Not Configured' %}
                        <span class="badge bg-secondary status-badge">UNCONFIGURED</span>
                    {% else %}
                        <span class="badge bg-danger status-badge">OFFLINE</span>
                    {% endif %}
                </div>
                <h6 class="card-subtitle text-muted text-uppercase small fw-bold mb-1">{{ service }}</h6>
                <h5 class="card-title mb-0">
                    {% if service == 'radarr' %}
                        {{ radarr_queue_count }} in Queue
                    {% elif service == 'sonarr' %}
                        {{ sonarr_queue_count }} in Queue
                    {% else %}
                        {{ data.status }}
                    {% endif %}
                </h5>
                <p class="small text-muted mt-2 mb-0 text-truncate">{{ data.details|default:"No additional info" }}</p>
            </div>
            {% if data.url %}
            <div class="card-footer bg-transparent border-0 p-0">
                <a href="{{ data.url }}" target="_blank" class="btn btn-dark w-100 rounded-0 rounded-bottom py-2 small">
                    Open <i class="bi bi-box-arrow-up-right ms-1"></i>
                </a>
            </div>
            {% endif %}
        </div>
    </div>
    {% endfor %}
</div>

<!-- Recent Media Row -->
{% if recent_media %}
<div class="mb-5">
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="fw-bold mb-0">Recently Added</h4>
        <a href="{{ jellyfin_url }}" target="_blank" class="text-danger text-decoration-none small">View All <i class="bi bi-chevron-right"></i></a>
    </div>
    <div class="row row-cols-2 row-cols-md-3 row-cols-lg-6 g-3">
        {% for item in recent_media %}
        <div class="col">
            <div class="card media-card h-100">
                {% if item.ImageTags.Primary %}
                    <img src="{{ jellyfin_url }}/Items/{{ item.Id }}/Images/Primary?maxWidth=400&tag={{ item.ImageTags.Primary }}" class="card-img-top media-img" alt="{{ item.Name }}">
                {% else %}
                    <div class="bg-dark d-flex align-items-center justify-content-center media-img">
                        <i class="bi bi-film fs-1 text-secondary"></i>
                    </div>
                {% endif %}
                <div class="card-body p-2">
                    <p class="card-text small fw-bold text-truncate mb-0">{{ item.Name }}</p>
                    <p class="card-text text-muted" style="font-size: 0.7rem;">{{ item.ProductionYear|default:"" }}</p>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
{% endif %}

<div class="row g-4 mb-5">
    <!-- Jellyseerr Requests -->
    <div class="col-lg-6">
        <div class="card h-100 border-0">
            <div class="card-header bg-transparent border-0 pt-4 px-4">
                <h5 class="fw-bold mb-0">Pending Requests</h5>
            </div>
            <div class="card-body p-4">
                {% if pending_requests %}
                    <ul class="list-group list-group-flush bg-transparent">
                        {% for req in pending_requests %}
                        <li class="list-group-item bg-transparent border-secondary px-0 py-3">
                            <div class="d-flex align-items-center">
                                <div class="flex-grow-1">
                                    <h6 class="mb-1 fw-bold">{{ req.media.title|default:req.media.name }}</h6>
                                    <p class="small text-muted mb-0">Requested by {{ req.requestedBy.displayName }}</p>
                                </div>
                                <span class="badge {% if req.status == 1 %}bg-warning text-dark{% elif req.status == 2 %}bg-success{% else %}bg-secondary{% endif %} rounded-pill">
                                    {% if req.status == 1 %}Pending{% elif req.status == 2 %}Approved{% else %}Processing{% endif %}
                                </span>
                            </div>
                        </li>
                        {% endfor %}
                    </ul>
                {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-inbox fs-1 text-muted d-block mb-2"></i>
                        <p class="text-muted">No pending requests</p>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- Streaming Info -->
    <div class="col-lg-6">
        <div class="card h-100 border-0 bg-danger bg-opacity-10">
            <div class="card-body p-4 d-flex flex-column justify-content-center align-items-center text-center">
                <i class="bi bi-cast fs-1 text-danger mb-3"></i>
                <h4 class="fw-bold">Ready to Stream?</h4>
                <p class="mb-4">Use the cast button in the header or your mobile app to send media directly to your TV.</p>
                <div class="bg-dark bg-opacity-50 p-3 rounded text-start w-100">
                    <small class="d-block mb-1 text-danger fw-bold"><i class="bi bi-info-circle-fill me-1"></i> Pro Tip:</small>
                    <small class="text-muted">For the best experience, ensure your server is connected via Ethernet and your playback device is on a 5GHz Wi-Fi band.</small>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    // Initialize Chromecast
    window.__onGCastApiAvailable = function(isAvailable) {
        if (isAvailable) {
            initializeCastApi();
        }
    };

    function initializeCastApi() {
        cast.framework.CastContext.getInstance().setOptions({
            receiverApplicationId: chrome.cast.media.DEFAULT_RECEIVER_APP_ID,
            autoJoinPolicy: chrome.cast.AutoJoinPolicy.ORIGIN_SCOPED
        });
    }
</script>
{% endblock %}
