@model RssListViewModel

<div class="rss-list">
    <div class="rss-header">
        <div onclick="sortBy('title')" class="@(Model.CurrentSort == "title" ? "active" : "")">Titel</div>
        <div onclick="sortBy('date')" class="@(Model.CurrentSort == "date" ? "active" : "")">Tid / Datum</div>
        <div onclick="sortBy('seeders')" class="@(Model.CurrentSort == "seeders" ? "active" : "")">Seeders</div>
        <div onclick="sortBy('leechers')" class="@(Model.CurrentSort == "leechers" ? "active" : "")">Leechers</div>
        <div>Åtgärd</div>
    </div>

    @foreach (var group in Model.Items
        .GroupBy(t => new { t.MovieName, t.Metadata?.Year })
        .Select(g => new
        {
            MovieName = g.Key.MovieName,
            Year = g.Key.Year,
            Versions = g.OrderByDescending(t => t.Title.Contains("Fix") || t.Title.Contains("Repack"))
        .ThenByDescending(t => t.Seeders)
        .ToList()
        }))
    {
        var main = group.Versions.First();
        var lastVersion = group.Versions.Last();

        <!-- Huvudrad -->
        <div class="rss-row rss-group-title @(group.Versions.Count == 1 ? "last-row" : "")">
            <div class="col-title">
                @if (!string.IsNullOrEmpty(main.Metadata?.Poster) && main.Metadata.Poster != "N/A")
                {
                    <a href="@main.Metadata.Poster" class="glightbox">
                        <img src="@main.Metadata.Poster"
                             alt="@main.MovieName"
                             class="poster"
                             onerror="this.onerror=null; this.src='/images/fallback.jpg';" />
                    </a>
                }
                else
                {
                    <img src="/images/fallback.jpg" alt="@main.MovieName" class="poster placeholder" />
                }

                <div class="title-info">
                    <strong>@(main.Metadata?.Title ?? group.MovieName) (@group.Year)</strong>

                    @if (main.IsNew)
                    {
                        <img src="/images/new.png" alt="New" class="badge" />
                    }

                    <div class="meta">
                        @if (!string.IsNullOrEmpty(main.Metadata?.Genre))
                        {
                            <span class="genre">@main.Metadata.Genre</span>
                        }
                        @if (!string.IsNullOrEmpty(main.Metadata?.ImdbID))
                        {
                            <a class="imdb" href="https://www.imdb.com/title/@main.Metadata.ImdbID" target="_blank">
                                ⭐ @main.Metadata.ImdbRating
                            </a>
                        }
                    </div>
                </div>
            </div>

            <div class="col-date">
                <div class="time">@main.PublishDate.ToString("HH:mm")</div>
                <div class="date">@main.PublishDate.ToString("yyyy-MM-dd")</div>
            </div>
            <div class="col-center @(main.Seeders > 40 ? "highlight-green" : "")">@main.Seeders</div>
            <div class="col-center highlight-red">@main.Leechers</div>
            <div class="col-action">
                <form asp-controller="RSS" asp-action="Add" method="post" onsubmit="return confirmDownload('@main.Title')">
                    <input type="hidden" name="rssUrl" value="@main.RssUrl" />
                    <button type="submit" class="btn-add btn-small">➕ Lägg till</button>
                </form>
            </div>
        </div>

        <!-- Versioner -->
        @if (group.Versions.Count > 1)
        {
            foreach (var version in group.Versions.Skip(1))
            {
                var isLast = version == lastVersion;
                <div class="rss-row rss-version @(isLast ? "last-version" : "")" title="@version.Title">
                    <div class="col-title">
                        <strong>@version.Title</strong>
                    </div>
                    <div>@version.PublishDate.ToString("HH:mm yyyy-MM-dd")</div>
                    <div class="@(version.Seeders > 40 ? "highlight-green" : "")">@version.Seeders</div>
                    <div class="highlight-red">@version.Leechers</div>
                    <div class="col-action">
                        <form asp-controller="RSS" asp-action="Add" method="post" onsubmit="return confirmDownload('@version.Title')">
                            <input type="hidden" name="rssUrl" value="@version.RssUrl" />
                            <button type="submit" class="btn-add btn-small">➕</button>
                        </form>
                    </div>
                </div>
            }
        }
    }

    <!-- Pagination -->
    <div class="pagination">
        @for (int i = 1; i <= Model.TotalPages; i++)
        {
            if (i == Model.CurrentPage)
            {
                <span class="current">@i</span>
            }
            else
            {
                <a href="javascript:void(0)" onclick="loadRss('?page=@i')">@i</a>
            }
        }
    </div>
</div>
