import logging
from pathlib import Path

import bottle

from personal_data import csv_import

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

ROOT_DIRECTORY = Path('output')


@bottle.route('/<csv_type>/newest')
def newest_entry(csv_type: str):
    """
    Loads a CSV file (default: data.csv, overridable by query param 'file'),
    finds the newest entry based on the 'time.current' column, and returns it as JSON.
    """

    path = ROOT_DIRECTORY / f'{csv_type}.csv'

    bottle.response.content_type = 'application/json'

    try:
        data = csv_import.load_csv_file(path)
    except Exception:
        logger.exception('Error loading CSV file at %s', path)
        bottle.response.status = 500
        return {'error': 'Failed to load CSV'}

    if not data:
        bottle.response.status = 404
        return {'error': 'CSV file is empty or no data found'}

    TIME_COLUMN = 'time.current'

    if TIME_COLUMN in data[0]:
        newest = max(data, key=lambda r: r.get(TIME_COLUMN))
    else:
        newest = data[-1]

    return {
        csv_import.csv_safe_value(k): csv_import.csv_safe_value(v)
        for k, v in newest.items()
    }


if __name__ == '__main__':
    bottle.run(host='localhost', port=8080, debug=True)