"""Fill in a module description here"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/00_spec.ipynb.

# %% auto 0
__all__ = ['stripe_openapi_url', 'stripe_endpoints', 'update_version']

# %% ../nbs/00_spec.ipynb 2
from fastcore.all import *
from fastcore.script import *

import httpx, json

# %% ../nbs/00_spec.ipynb 4
stripe_openapi_url = 'https://raw.githubusercontent.com/stripe/openapi/refs/heads/master/openapi/spec3.json'

# %% ../nbs/00_spec.ipynb 16
def stripe_endpoints(spec: dict):
    'Extracts all the endpoints and their parameters from the Stripe OpenAPI spec.'
    endpoints = []
    for path, methods in spec['paths'].items():
        for verb, details in methods.items():
            op_id = details.get('operationId', '')
            summary = details.get('summary', '')
            query_params = [dict(name=p['name'], description=p.get('description', ''))
                            for p in details.get('parameters', []) if p.get('in') == 'query']
            body_params = []
            if 'requestBody' in details:
                schema = nested_idx(details, 'requestBody', 'content', 'application/x-www-form-urlencoded', 'schema', 'properties') or {}
                body_params = [dict(name=k, description=v.get('description', '')) for k,v in schema.items()]
            all_params = query_params + body_params
            endpoints.append(dict(path=path, verb=verb, op_id=op_id, summary=summary, params=all_params))
    return endpoints

# %% ../nbs/00_spec.ipynb 19
@call_parse
def update_version():
    'Update the version to the latest version of the Stripe API and the endpoints file.'
    cfg = Config.find("settings.ini")
    stripe_spec = httpx.get(stripe_openapi_url).json()
    stripe_version = stripe_spec['info']['version'].split('.')[0].replace('-', '.')

    if cfg.d['version'] == stripe_version: return
    cfg.d['version'] = stripe_version
    cfg.save()
    eps = stripe_endpoints(stripe_spec)
    (cfg.config_path/'faststripe/endpoints.py').write_text(f'eps = {json.dumps(eps, indent=4)}')
    print(f'Updated version to {stripe_version}')
