# Copyright (C) 2018-2020  The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU Affero General Public License version 3, or any later version
# See top-level LICENSE file for more information

from swh.web.api.apidoc import api_doc, format_docstring
from swh.web.api.apiurls import api_route
from swh.web.common import service
from swh.web.common.exc import LargePayloadExc
from swh.web.common.identifiers import (
    resolve_swhid,
    get_swhid,
    group_swhids,
)


@api_route(r"/resolve/(?P<swhid>.*)/", "api-1-resolve-swhid")
@api_doc("/resolve/")
@format_docstring()
def api_resolve_swhid(request, swhid):
    """
    .. http:get:: /api/1/resolve/(swhid)/

        Resolve :ref:`persistent-identifiers`.

        Try to resolve a provided SWHID into an url for browsing the
        pointed archive object. If the provided identifier is valid,
        the existence of the object in the archive will also be checked.

        :param string swhid: a SoftWare Heritage persistent IDentifier

        :>json string browse_url: the url for browsing the pointed object
        :>json object metadata: object holding optional parts of the SWHID
        :>json string namespace: the SWHID namespace
        :>json string object_id: the hash identifier of the pointed object
        :>json string object_type: the type of the pointed object
        :>json number scheme_version: the scheme version of the SWHID

        {common_headers}

        :statuscode 200: no error
        :statuscode 400: an invalid SWHID has been provided
        :statuscode 404: the pointed object does not exist in the archive

        **Example:**

        .. parsed-literal::

            :swh_web_api:`resolve/swh:1:rev:96db9023b881d7cd9f379b0c154650d6c108e9a3;origin=https://github.com/openssl/openssl/`
    """
    # try to resolve the provided swhid
    swhid_resolved = resolve_swhid(swhid)
    # id is well-formed, now check that the pointed
    # object is present in the archive, NotFoundExc
    # will be raised otherwise
    swhid_parsed = swhid_resolved["swhid_parsed"]
    object_type = swhid_parsed.object_type
    object_id = swhid_parsed.object_id
    service.lookup_object(object_type, object_id)
    # id is well-formed and the pointed object exists
    swhid_data = swhid_parsed.to_dict()
    swhid_data["browse_url"] = request.build_absolute_uri(swhid_resolved["browse_url"])
    return swhid_data


@api_route(r"/known/", "api-1-known", methods=["POST"])
@api_doc("/known/")
@format_docstring()
def api_swhid_known(request):
    """
    .. http:post:: /api/1/known/

        Check if a list of objects are present in the Software Heritage
        archive.

        The objects to check existence must be provided using
        :ref:`persistent-identifiers`.

        :<jsonarr string -: input array of SWHIDs, its length can not exceed 1000.

        :>json object <swhid>: an object whose keys are input SWHIDs and values
            objects with the following keys:

                * **known (bool)**: whether the object was found

        {common_headers}

        :statuscode 200: no error
        :statuscode 400: an invalid SWHID was provided
        :statuscode 413: the input array of SWHIDs is too large

    """
    limit = 1000
    if len(request.data) > limit:
        raise LargePayloadExc(
            "The maximum number of SWHIDs this endpoint can receive is %s" % limit
        )

    swhids = [get_swhid(swhid) for swhid in request.data]

    response = {str(swhid): {"known": False} for swhid in swhids}

    # group swhids by their type
    swhids_by_type = group_swhids(swhids)
    # search for hashes not present in the storage
    missing_hashes = service.lookup_missing_hashes(swhids_by_type)

    for swhid in swhids:
        if swhid.object_id not in missing_hashes:
            response[str(swhid)]["known"] = True

    return response

@@ -740,22 +740,36 @@
 Resolve 
-:ref:%60
+a SoftWare Heritage 
 persiste
@@ -766,35 +766,35 @@
 e persistent
--id
+ ID
 entifier
 s%60.%0A%0A       
@@ -773,35 +773,40 @@
 stent IDentifier
-s%60.
+ (SWHID)
 %0A%0A        Try to
@@ -825,21 +825,154 @@
 rovided 
-SWHID
+%60SoftWare Heritage persistent IDentifier%0A        %3Chttps://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html%3E%60_%0A       
  into an
@@ -992,24 +992,16 @@
 sing the
-%0A       
  pointed
@@ -1016,16 +1016,25 @@
  object.
+%0A%0A       
  If the 
@@ -1062,24 +1062,16 @@
 s valid,
-%0A       
  the exi
@@ -1089,24 +1089,32 @@
 he object in
+%0A       
  the archive
@@ -2943,14 +2943,109 @@
     
-:ref:%60
+%60SoftWare Heritage persistent IDentifiers%0A        %3Chttps://docs.softwareheritage.org/devel/swh-model/
 pers
@@ -3062,17 +3062,24 @@
 ntifiers
-%60
+.html%3E%60_
 .%0A%0A     

Fix endpoints documentation rendering issue