from http import HTTPStatus
from typing import Iterable, Union, Mapping

from flask import request
from flask_restful import Resource, fields, marshal

from metadata_service.proxy import get_proxy_client

popular_table_fields = {
    'database': fields.String,
    'cluster': fields.String,
    'schema': fields.String,
    'table_name': fields.String(attribute='name'),
    'table_description': fields.String(attribute='description'),  # Optional
}

popular_tables_fields = {
    'popular_tables': fields.List(fields.Nested(popular_table_fields))
}


class PopularTablesAPI(Resource):
    """
    PopularTables API
    """
    def __init__(self) -> None:
        self.client = get_proxy_client()

    def get(self) -> Iterable[Union[Mapping, int, None]]:
        limit = request.args.get('limit', 10)
        popular_tables = self.client.get_popular_tables(num_entries=limit)
        return marshal({'popular_tables': popular_tables}, popular_tables_fields), HTTPStatus.OK

[(13, 'flask_restful.fields.String', 'fields.String', False, 'from flask_restful import Resource, fields, marshal\n'), (14, 'flask_restful.fields.String', 'fields.String', False, 'from flask_restful import Resource, fields, marshal\n'), (18, 'flask_restful.fields.Nested', 'fields.Nested', False, 'from flask_restful import Resource, fields, marshal\n'), (27, 'metadata_service.proxy.get_proxy_client', 'get_proxy_client', False, 'from metadata_service.proxy import get_proxy_client\n'), (30, 'flask.request.args.get', 'request.args.get', False, 'from flask import request\n'), (32, 'flask_restful.marshal', 'marshal', False, 'from flask_restful import Resource, fields, marshal\n')]