import json

with open(r'docs/openapi_検証.json', encoding='utf-8') as f:
    api = json.load(f)

def resolve_ref(ref):
    parts = ref.lstrip('#/').split('/')
    obj = api
    for p in parts:
        obj = obj[p]
    return obj

def resolve_schema(schema, depth=0):
    if depth > 3:
        return schema
    if '$ref' in schema:
        return resolve_schema(resolve_ref(schema['$ref']), depth+1)
    if schema.get('type') == 'array' and 'items' in schema:
        schema = dict(schema)
        schema['items'] = resolve_schema(schema['items'], depth+1)
    if 'properties' in schema:
        schema = dict(schema)
        schema['properties'] = {k: resolve_schema(v, depth+1) for k, v in schema['properties'].items()}
    return schema

# /post/lists の200レスポンス
resp = api['paths']['/post/lists']['get']['responses']['200']
schema = resolve_schema(resp['content']['application/json']['schema'])
print('=== GET /post/lists Response 200 ===')
print(json.dumps(schema, ensure_ascii=False, indent=2)[:3000])

print()

# /post/{post_id} の200レスポンス
resp2 = api['paths']['/post/{post_id}']['get']['responses']['200']
schema2 = resolve_schema(resp2['content']['application/json']['schema'])
print('=== GET /post/{post_id} Response 200 ===')
print(json.dumps(schema2, ensure_ascii=False, indent=2)[:4000])

print()

# serversの確認
print('=== Servers ===')
print(json.dumps(api.get('servers', []), ensure_ascii=False, indent=2))

# componentsのparametersでよく使うものを確認
print()
print('=== Key Parameters ===')
for key in ['Authorization', 'date_from', 'date_to', 'limit_50', 'offset']:
    param = api['components']['parameters'].get(key, {})
    print(f'{key}: in={param.get("in")}, name={param.get("name")}, schema={param.get("schema")}')
