"""
Module to convert json metadata into rst files.

All of the information about the transformed pudl tables, namely their fields
types and descriptions, resides in the datapackage metadata. This module makes
that information available to users, without duplicating any data, by converting
json metadata files into documentation-compatible rst files. The functions
serve to extract the field names, field data types, and field descriptions of
each pudl table and outputs them in a manner that automatically updates the
read-the-docs.

datapky2rst_many() reads the metadata json file, extracts the afformentioned
data, and puts it into an rst file with tags and titles for each of the unique
tables. This way, these tags can be referenced and appear in other rst files.

"""

import json
import logging
import sys
from pathlib import Path

from jinja2 import Template

logger = logging.getLogger(__name__)


path = Path.cwd() / 'src/pudl/package_data/meta/datapkg/datapackage.json'
metadata = open(path)
metadata_json = json.load(metadata)


def datapkg2rst_one():
    """Convert json metadata to a single rst file."""
    logger.info("Converting json metadata into an rst file")
    template = Template(rst_template_one)
    rendered = template.render(metadata_json)
    # Create or overwrite an rst file containing the field descriptions of the input table
    with open('docs/data_sources/metadata.rst', 'w') as f:
        f.seek(0)  # Used to overwrite exisiting content
        f.write(rendered)
        f.truncate()  # Used to overwrite exisiting content


def datapkg2rst_many(table):
    """Convert json metadata to multiple rst files."""
    logger.info("Converting json metadata into rst files")
    template = Template(rst_template_many)
    rendered = template.render(metadata_json, table=table).strip('\n') + ('\n')
    # Create or overwrite an rst file containing the field descriptions of the input table
    with open(f'docs/data_sources/{table}.rst', 'w') as f:
        f.seek(0)  # Used to overwrite exisiting content
        f.write(rendered)
        f.truncate()  # Used to overwrite exisiting content


def main():
    """Run conversion from json to rst."""
    datapkg2rst_one()


if __name__ == '__main__':
    sys.exit(main())

# ----------------------------------------------------------------------------
# T E M P L A T E S
# ----------------------------------------------------------------------------

"""
The following templates map json data into a single rst file (rst_template_one)
per table or one long rst file seperated by table titles and document links
(rst_template_many).

It's important for the templates that the json data do not contain execess
white space either at the beginning or the end of each value.
"""

# Template for all tables in one rst file
rst_template_one = '''
{% for resource in resources %}
.. _{{ resource.name }}:

Contents of {{ resource.name }} table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. list-table::

  * - **Field**
    - **Data Type**
    - **Description**
{% for field in resource.schema.fields %}
  * - {{ field.name }}
    - {{ field.type }}{% if field.description %}
    - {{ field.description }}{% else %}
    - N/A{% endif %}
{% endfor %}
{% endfor %}
'''

# Template for one table per rst file
rst_template_many = '''
{% for resource in resources %}
{% if resource.name == table %}
Contents of {{ resource.name }}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. list-table::
   :widths: 15 10 30
   :header-rows: 1

  * - **Field**
    - **Data Type**
    - **Description**
{% for field in resource.schema.fields %}
  * - {{ field.name }}
    - {{ field.type }}
    - {{ field.description }}
{% endfor %}
{% endif %}
{% endfor %}
'''

@@ -2470,63 +2470,8 @@
 ata 
-into a single rst file (rst_template_one)%0Aper table or 
 one 
@@ -2502,17 +2502,17 @@
 by table
- 
+%0A
 titles a
@@ -2528,17 +2528,67 @@
 nt links
-%0A
+ (rst_template_one) or a single rst file%0Aper table 
 (rst_tem
@@ -2598,17 +2598,16 @@
 te_many)
-.
 %0A%0AIt's i

fix one/many language mix up in template descriptions