""".. title:: Search for NWS Text Products over a time range

Return to `API Services </api/#json>`_

Documentation for /json/nwstext_search.py
-----------------------------------------

This legacy service returns a JSON response of NWS Text Products over a time
range.

Changelog
---------

- 2024-07-25: Initial documentation

Example Usage
-------------

Return all Des Moines Area Forecast Discussions for 2024

https://mesonet.agron.iastate.edu/json/nwstext_search.py?awipsid=AFDDMX&\
sts=2024-01-01T00:00Z&ets=2025-01-01T00:00Z

Return all Severe Thunderstorm Warnings for 10 July 2024 UTC

https://mesonet.agron.iastate.edu/json/nwstext_search.py?awipsid=SVR&\
sts=2024-07-10T00:00Z&ets=2024-07-11T00:00Z

"""

import json
from datetime import datetime
from typing import Annotated

from pydantic import Field
from pyiem.database import get_sqlalchemy_conn, sql_helper
from pyiem.webutil import CGIModel, iemapp

from iemweb.fields import CALLBACK_FIELD
from iemweb.util import json_response_dict


class Schema(CGIModel):
    """See how we are called."""

    callback: CALLBACK_FIELD = None
    awipsid: Annotated[
        str,
        Field(
            description="The AWIPS Identifier to search for",
            max_length=6,
            min_length=3,
        ),
    ] = "AFDDMX"
    sts: Annotated[
        datetime,
        Field(description="Start of the time period (UTC) to search for"),
    ]
    ets: Annotated[
        datetime,
        Field(description="End of the time period (UTC) to search for"),
    ]


def run(query: Schema) -> str:
    """Actually do some work!"""

    data = json_response_dict({"results": []})
    pillimit = "pil"
    if len(query.awipsid) == 3:
        pillimit = "substr(pil, 1, 3) "
    with get_sqlalchemy_conn("afos") as conn:
        res = conn.execute(
            sql_helper(
                """
        SELECT data,
        to_char(entered at time zone 'UTC', 'YYYY-MM-DDThh24:MIZ'),
        source, wmo from products WHERE
        entered >= :sts and entered < :ets and {pillimit} =:awipsid
        ORDER by entered ASC
        """,
                pillimit=pillimit,
            ),
            {"sts": query.sts, "ets": query.ets, "awipsid": query.awipsid},
        )
        for row in res:
            data["results"].append(
                dict(ttaaii=row[3], utcvalid=row[1], data=row[0], cccc=row[2])
            )
    return json.dumps(data)


def get_mckey(environ: dict) -> str:
    """Get the key."""
    return (
        f"/json/nwstext_search/{environ['sts']:%Y%m%d%H%M}/"
        f"{environ['ets']:%Y%m%d%H%M}/{environ['awipsid']}"
    ).strip()


@iemapp(
    help=__doc__,
    memcachekey=get_mckey,
    memcacheexpire=120,
    schema=Schema,
    default_tz="UTC",
)
def application(environ: dict, start_response: callable):
    """Answer request."""
    query: Schema = environ["_cgimodel_schema"]
    res = run(query)
    start_response("200 OK", [("Content-type", "application/json")])
    return res
