{% extends "base.html" %}
{% block footer %}{% endblock %}
{% block extra_head %}
<link rel="stylesheet" href="{{ static_url('/static/css/landing.css') }}">
<link rel="stylesheet" href="{{ static_url('/static/css/news.css') }}">
{% endblock %}
{% block content %}
<div class="landing">
    {% if user %}
    <div class="dashboard">
        <div class="dashboard-top">
            <div class="dashboard-actions">
                <div class="dashboard-welcome">
                    {% set _user = user %}{% set _size = 48 %}{% set _size_class = "md" %}{% include "_avatar_link.html" %}
                    <span class="dashboard-greeting">Welcome back, <strong>{{ user['username'] }}</strong></span>
                </div>
                <div class="dashboard-buttons">
                    <a href="/feed" class="dashboard-btn dashboard-btn-primary">New Post</a>
                    <a href="/game" class="dashboard-btn">Code Farm</a>
                    <a href="/projects" class="dashboard-btn">Projects</a>
                    <a href="/gists" class="dashboard-btn">Gists</a>
                </div>
            </div>
            <div class="dashboard-stats">
                <div class="dashboard-stat">
                    <span class="dashboard-stat-value">{{ user_post_count }}</span>
                    <span class="dashboard-stat-label">Posts</span>
                </div>
                <div class="dashboard-stat">
                    <span class="dashboard-stat-value">{{ user_stars }}</span>
                    <span class="dashboard-stat-label">Stars</span>
                </div>
                <div class="dashboard-stat dashboard-stat-level">
                    <span class="dashboard-stat-value">Lv {{ user_level }}</span>
                    <span class="dashboard-stat-label">Level</span>
                    <div class="dashboard-xp-bar" role="progressbar" aria-valuenow="{{ xp_progress_pct }}" aria-valuemin="0" aria-valuemax="100" aria-label="XP progress to next level">
                        <div class="dashboard-xp-fill" style="width:{{ xp_progress_pct }}%"></div>
                    </div>
                    <span class="dashboard-xp-label">{{ xp_progress_pct }}% to Lv {{ user_level + 1 }}</span>
                </div>
            </div>
        </div>
        <div class="dashboard-main">
            <div class="dashboard-feed">
                {% if landing_posts %}
                <div class="dashboard-feed-header">
                    <h2>Latest Posts</h2>
                    <a href="/feed" class="dashboard-feed-link">Browse all &rarr;</a>
                </div>
                <div class="dashboard-posts-grid">
                    {% for item in landing_posts %}
                    <article class="dashboard-post-card card-link-host">
                        {% set _href = "/posts/" ~ item.slug %}
                        {% set _label = item.post.get('title') or 'View post' %}
                        {% include "_card_link.html" %}
                        <div class="dashboard-post-header">
                            {% set _user = item.author %}
                            {% set _size = 28 %}
                            {% set _size_class = "sm" %}
                            {% include "_avatar_link.html" %}
                            <div class="dashboard-post-author-wrap">
                                {% set _user = item.author %}{% set _class = "dashboard-post-author" %}{% include "_user_link.html" %}
                                <span class="dashboard-post-time">{{ dt_ago(item.created_at) if item.created_at else item.time_ago }}</span>
                            </div>
                            <span class="badge badge-{{ item.post['topic'] }}">{{ item.post['topic'] }}</span>
                        </div>
                        {% if item.post.get('title') %}
                        <h3 class="dashboard-post-title">
                            <a href="/posts/{{ item.slug }}">{{ render_title(item.post['title'], author_is_admin=is_admin(item.author)) }}</a>
                        </h3>
                        {% endif %}
                        <div class="dashboard-post-content rendered-content">{{ render_content(item.post['content'][:200] ~ ('...' if item.post['content']|length > 200 else ''), author_is_admin=is_admin(item.author)) }}</div>
                        <div class="dashboard-post-footer">
                            <span class="dashboard-post-stat">+{{ item.stars }}</span>
                            <span class="dashboard-post-stat">&#x1F4AC; {{ item.comment_count }}</span>
                            <a href="/posts/{{ item.slug }}" class="dashboard-post-open">Open &rarr;</a>
                        </div>
                    </article>
                    {% endfor %}
                </div>
                {% endif %}
            </div>
            <aside class="dashboard-sidebar">
                {% if top_contributors %}
                <div class="dashboard-sidebar-card">
                    <h3 class="dashboard-sidebar-heading">Top Contributors</h3>
                    <ul class="dashboard-contributor-list">
                        {% for author in top_contributors %}
                        <li class="dashboard-contributor">
                            {% set _user = author %}{% set _size = 28 %}{% set _size_class = "sm" %}{% include "_avatar_link.html" %}
                            <div class="dashboard-contributor-meta">
                                {% set _user = author %}{% set _class = "dashboard-contributor-name" %}{% include "_user_link.html" %}
                                <span class="dashboard-contributor-stars">{{ author.get('stars', 0) }} stars</span>
                            </div>
                        </li>
                        {% endfor %}
                    </ul>
                    <a href="/leaderboard" class="dashboard-sidebar-link">View leaderboard &rarr;</a>
                </div>
                {% endif %}
                {% if trending_topics %}
                <div class="dashboard-sidebar-card">
                    <h3 class="dashboard-sidebar-heading">Trending Topics</h3>
                    <div class="dashboard-tag-cloud">
                        {% for item in trending_topics %}
                        <a href="/feed?topic={{ item.topic }}" class="dashboard-tag">
                            <span class="badge badge-{{ item.topic }}">{{ item.topic }}</span>
                            <span class="dashboard-tag-count">{{ item.count }}</span>
                        </a>
                        {% endfor %}
                    </div>
                </div>
                {% endif %}
            </aside>
        </div>
    </div>
    {% else %}
    <section class="landing-hero">
        <h1>Devplace.net - The Developer <span>Social Network</span></h1>
        <p>Track industry shifts. Discover bold releases. Share what you're building in an open environment built by developers, for developers.</p>
        <a href="/auth/signup" class="landing-cta"><span class="icon">&#x2728;</span> Join DevPlace Free</a>

        <div class="landing-features">
            <div class="landing-feature">
                <div class="landing-feature-icon">&#x1F511;</div>
                <h3>100% Free Forever</h3>
            </div>
            <div class="landing-feature">
                <div class="landing-feature-icon">&#x1F6AB;</div>
                <h3>Zero Ads</h3>
            </div>
            <div class="landing-feature">
                <div class="landing-feature-icon">&#x1F4AD;</div>
                <h3>No Gatekeeping</h3>
                <p>We never editorialise technical opinion. A short list of prohibited categories is enforced - read the <a href="/docs/community-guidelines.html">community guidelines</a>.</p>
            </div>
            <div class="landing-feature">
                <div class="landing-feature-icon">&#x1F4B3;</div>
                <h3>No Payments</h3>
            </div>
        </div>

        <div class="landing-auth-link">
            Already have an account? <a href="/auth/login">Log In</a>
        </div>
    </section>
    {% endif %}

    <section class="landing-game{% if user %} landing-game-dashboard{% endif %}" aria-labelledby="landing-game-heading">
        <div class="landing-game-glow"></div>
        <div class="landing-game-body">
            <span class="landing-game-eyebrow">New on DevPlace</span>
            <h2 id="landing-game-heading" class="landing-game-title">
                Code Farm <span>- grow software, not vegetables</span>
            </h2>
            <p class="landing-game-tagline">
                Plant projects that build in real time, harvest coins and XP, upgrade your CI pipeline,
                and unlock the prestige grind. Then water your friends' builds - or raid the ones who left
                a harvest sitting too long.
            </p>
            <div class="landing-game-crops" aria-hidden="true">
                <span title="Shell Script">&#x1F41A;</span>
                <span title="Python Script">&#x1F40D;</span>
                <span title="Web App">&#x1F4DC;</span>
                <span title="Go Service">&#x1F439;</span>
                <span title="Rust Engine">&#x1F980;</span>
                <span title="Compiler">&#x03BB;</span>
                <span title="Kernel">&#x2699;&#xFE0F;</span>
            </div>
            <ul class="landing-game-points">
                <li>7 project tiers, from 30-second scripts to 2-hour kernels</li>
                <li>CI upgrades, a four-branch perk tree, and stacking prestige boosts</li>
                <li>Co-op watering, PvP raids, daily streaks, quests, and a global leaderboard</li>
            </ul>
            {% if user %}
            <a href="/game" class="landing-game-cta"><span class="icon">&#x1F33E;</span> Play Code Farm &rarr;</a>
            {% else %}
            <a href="/auth/signup" class="landing-game-cta"><span class="icon">&#x1F33E;</span> Join free to play &rarr;</a>
            {% endif %}
        </div>
    </section>

    {% if landing_posts and not user %}
    <section class="landing-section" aria-labelledby="landing-posts-heading">
        <div class="landing-section-header">
            <h2 id="landing-posts-heading">Latest Posts</h2>
            <a href="/feed" class="landing-section-link">Browse all posts &rarr;</a>
        </div>
        <div class="landing-posts-grid">
            {% for item in landing_posts %}
            <article class="landing-post-card card-link-host">
                {% set _href = "/posts/" ~ item.slug %}
                {% set _label = item.post.get('title') or 'View post' %}
                {% include "_card_link.html" %}
                <div class="landing-post-header">
                    {% set _user = item.author %}
                    {% set _size = 28 %}
                    {% set _size_class = "sm" %}
                    {% include "_avatar_link.html" %}
                    <div class="landing-post-author-wrap">
                        {% set _user = item.author %}{% set _class = "landing-post-author" %}{% include "_user_link.html" %}
                        <span class="landing-post-time">{{ dt_ago(item.created_at) if item.created_at else item.time_ago }}</span>
                    </div>
                    <span class="badge badge-{{ item.post['topic'] }}">{{ item.post['topic'] }}</span>
                </div>
                {% if item.post.get('title') %}
                <h3 class="landing-post-title">
                    <a href="/posts/{{ item.slug }}">{{ render_title(item.post['title'], author_is_admin=is_admin(item.author)) }}</a>
                </h3>
                {% endif %}
                <div class="landing-post-content rendered-content">{{ render_content(item.post['content'][:200] ~ ('...' if item.post['content']|length > 200 else ''), author_is_admin=is_admin(item.author)) }}</div>
                <div class="landing-post-footer">
                    <a href="/posts/{{ item.slug }}" class="landing-post-open">Open &rarr;</a>
                </div>
            </article>
            {% endfor %}
        </div>
    </section>
    {% endif %}

    {% if landing_articles %}
    <section class="landing-section" aria-labelledby="landing-news-heading">
        <div class="landing-section-header">
            <h2 id="landing-news-heading">Developer News</h2>
            <a href="/news" class="landing-section-link">View all news &rarr;</a>
        </div>
        <div class="news-grid">
            {% for item in landing_articles %}
            <article class="news-card card-link-host">
                {% set _href = "/news/" ~ (item.slug or item.uid) %}
                {% set _label = item.title %}
                {% include "_card_link.html" %}
                {% if item.image_url %}
                <div class="news-card-image">
                    <img src="{{ item.image_url }}" alt="{{ item.title }}" loading="lazy" class="image-fallback">
                </div>
                {% endif %}
                <div class="news-card-body">
                    <div class="news-card-meta">
                        <span class="news-source">{{ item.source_name }}</span>
                        {% if is_admin(user) %}
                        {% if item.featured %}
                        <span class="news-featured-badge">&#x2605; Featured</span>
                        {% endif %}
                        {% if item.grade >= 9 %}
                        <span class="news-grade news-grade-top">Grade {{ item.grade }}</span>
                        {% elif item.grade >= 7 %}
                        <span class="news-grade news-grade-high">Grade {{ item.grade }}</span>
                        {% else %}
                        <span class="news-grade">Grade {{ item.grade }}</span>
                        {% endif %}
                        {% endif %}
                        <span class="news-time">{{ dt_ago(item.synced_at) if item.synced_at else item.time_ago }}</span>
                    </div>
                    <h3 class="news-card-title">
                        <a href="/news/{{ item.slug or item.uid }}">{{ render_title(item.title) }}</a>
                    </h3>
                    {% if item.description %}
                    <div class="news-card-desc rendered-content">{{ render_content(item.description[:250]) }}</div>
                    {% endif %}
                    <a href="{{ item.url }}" target="_blank" rel="noopener" class="news-read-link">
                        Read on {{ item.source_name }} &#x2197;
                    </a>
                </div>
            </article>
            {% endfor %}
        </div>
    </section>
    {% endif %}

    <section class="landing-section landing-help" aria-labelledby="landing-help-heading">
        <div class="landing-section-header">
            <h2 id="landing-help-heading">Build With Us</h2>
            <a href="/docs/index.html" class="landing-section-link">Browse the docs &rarr;</a>
        </div>
        <p class="landing-help-intro">DevPlace is built in the open and made to be extended. Read the docs, explore the API, file an issue, or let Devii - our in-platform AI developer - do the work with you. Everything you need to start contributing is one click away.</p>
        <div class="landing-help-grid">
            <div class="landing-help-card">
                <div class="landing-help-icon">&#x1F4DA;</div>
                <h3>Documentation</h3>
                <p>Guides, the design system, component references, and deep internals - tiered for newcomers and operators alike.</p>
                <a href="/docs/index.html" class="landing-help-link">Read the docs &rarr;</a>
            </div>
            <div class="landing-help-card">
                <div class="landing-help-icon">&#x1F9E9;</div>
                <h3>API Reference</h3>
                <p>A complete REST API with live OpenAPI schema. Build integrations, bots, and tools on top of every endpoint.</p>
                <div class="landing-help-links">
                    <a href="/swagger">Swagger UI</a>
                    <a href="/openapi.json">OpenAPI JSON</a>
                </div>
            </div>
            <div class="landing-help-card">
                <div class="landing-help-icon">&#x1F41B;</div>
                <h3>Contribute &amp; Report</h3>
                <p>Found a bug or have an idea? Open an issue and follow it through to resolution in the integrated tracker.</p>
                <a href="/issues" class="landing-help-link">Open the issue tracker &rarr;</a>
            </div>
            <div class="landing-help-card landing-help-card-lead">
                <div class="landing-help-icon">&#x1F916;</div>
                <h3>Meet Devii</h3>
                <p>Devii is your AI developer built into the platform. Ask it to write posts, manage projects, run containers, edit files, and guide you live on the page.</p>
                <button type="button" class="landing-help-cta" data-devii-open>Launch Devii</button>
                <a href="/devii/" class="landing-help-link">Open the full terminal &rarr;</a>
            </div>
        </div>
    </section>

    <footer class="landing-footer">
        {% include "_footer_links.html" %}
    </footer>
</div>
{% endblock %}
