# retoor <retoor@molodetz.nl>

from devplacepy import schemas
from devplacepy.docs_examples import schema_example, action_example
from ._shared import NON_BODY_ENDPOINTS


def _classify(ep):
    if ep["id"] in _PAGE_RESPONSES or ep["id"] in _ACTION_RESPONSES:
        return "negotiable"
    if ep.get("ajax"):
        return "ajax"
    if ep["id"] in NON_BODY_ENDPOINTS:
        return "none"
    return "json"


_PAGE_RESPONSES = {
    "auth-signup": schemas.AuthPageOut,
    "auth-login": schemas.AuthPageOut,
    "auth-forgot-password": schemas.AuthPageOut,
    "auth-reset-password": schemas.AuthPageOut,
    "home": schemas.LandingOut,
    "feed-list": schemas.FeedOut,
    "posts-detail": schemas.PostDetailOut,
    "projects-list": schemas.ProjectsOut,
    "projects-detail": schemas.ProjectDetailOut,
    "gists-list": schemas.GistsOut,
    "gists-detail": schemas.GistDetailOut,
    "news-list": schemas.NewsListOut,
    "news-detail": schemas.NewsDetailOut,
    "profile-detail": schemas.ProfileOut,
    "leaderboard": schemas.LeaderboardOut,
    "messages-inbox": schemas.MessagesOut,
    "notifications-list": schemas.NotificationsOut,
    "issues-list": schemas.IssuesOut,
    "issues-detail": schemas.IssueDetailOut,
    "issues-attachments-list": schemas.IssueAttachmentsOut,
    "bookmarks-saved": schemas.SavedOut,
    "admin-users": schemas.AdminUsersOut,
    "admin-news-list": schemas.AdminNewsOut,
    "admin-settings-get": schemas.AdminSettingsOut,
    "admin-audit-log": schemas.AuditLogOut,
    "admin-audit-event": schemas.AuditEventOut,
    "admin-bots-monitor": schemas.AdminBotsOut,
    "containers-admin-edit-page": schemas.AdminContainerEditOut,
}

_ACTION_RESPONSES = {
    "auth-signup-post": ("/feed", {"username": "alice"}),
    "auth-login-post": ("/feed", None),
    "auth-forgot-password-post": ("/auth/forgot-password?sent=1", None),
    "auth-reset-password-post": ("/auth/login", None),
    "auth-logout": ("/", None),
    "posts-create": (
        "/posts/POST_SLUG",
        {"uid": "POST_UID", "slug": "POST_SLUG", "url": "/posts/POST_SLUG"},
    ),
    "posts-edit": (
        "/posts/POST_SLUG",
        {"uid": "POST_UID", "slug": "POST_SLUG", "url": "/posts/POST_SLUG"},
    ),
    "posts-delete": ("/feed", None),
    "comments-create": (
        "/posts/POST_SLUG#comment-COMMENT_UID",
        {"uid": "COMMENT_UID", "url": "/posts/POST_SLUG#comment-COMMENT_UID"},
    ),
    "comments-delete": (
        "/posts/POST_SLUG",
        {"deleted_uid": "COMMENT_UID", "target_type": "post", "target_uid": "TARGET_UID"},
    ),
    "projects-create": (
        "/projects/PROJECT_SLUG",
        {"uid": "PROJECT_UID", "slug": "PROJECT_SLUG", "url": "/projects/PROJECT_SLUG"},
    ),
    "projects-delete": ("/projects", None),
    "gists-create": (
        "/gists/GIST_SLUG",
        {"uid": "GIST_UID", "slug": "GIST_SLUG", "url": "/gists/GIST_SLUG"},
    ),
    "gists-edit": (
        "/gists/GIST_SLUG",
        {"uid": "GIST_UID", "slug": "GIST_SLUG", "url": "/gists/GIST_SLUG"},
    ),
    "gists-delete": ("/gists", None),
    "profile-update": ("/profile/YOUR_USERNAME", None),
    "profile-customization-global": (
        "/profile/YOUR_USERNAME",
        {"url": "/profile/YOUR_USERNAME", "cust_disable_global": True},
    ),
    "profile-customization-pagetype": (
        "/profile/YOUR_USERNAME",
        {"url": "/profile/YOUR_USERNAME", "cust_disable_pagetype": True},
    ),
    "follow-user": ("/profile/bob_test", None),
    "unfollow-user": ("/profile/bob_test", None),
    "messages-send": ("/messages?with_uid=RECEIVER_UID", {"uid": "MESSAGE_UID"}),
    "notifications-open": ("/posts/POST_SLUG#comment-COMMENT_UID", None),
    "notifications-mark-read": ("/notifications", None),
    "notifications-mark-all-read": ("/notifications", None),
    "issues-comment": ("/issues/12", {"comment_id": 1}),
    "issues-status": ("/issues/12", {"state": "closed"}),
    "admin-user-role": ("/admin/users", None),
    "admin-user-password": ("/admin/users", None),
    "admin-user-toggle": ("/admin/users", None),
    "admin-news-toggle": ("/admin/news", None),
    "admin-news-publish": ("/admin/news", None),
    "admin-news-landing": ("/admin/news", None),
    "admin-news-delete": ("/admin/news", None),
    "admin-settings": ("/admin/settings", None),
}


def _apply_negotiated_responses(groups):
    for group in groups:
        for ep in group.get("endpoints", []) or []:
            if ep.get("sample_response") is None:
                if ep["id"] in _PAGE_RESPONSES:
                    ep["sample_response"] = schema_example(_PAGE_RESPONSES[ep["id"]])
                elif ep["id"] in _ACTION_RESPONSES:
                    redirect, data = _ACTION_RESPONSES[ep["id"]]
                    ep["sample_response"] = action_example(redirect, data)
            ep["negotiation"] = _classify(ep)
