from datetime import datetime
from fastapi import APIRouter
from app.utils import declension
from fastapi_utils.tasks import repeat_every

from app.config import config
from app.data_getter import DataGetter

router = APIRouter()
data = DataGetter()

@router.on_event('startup')
@repeat_every(seconds=7200)
def start():
    data.fetch()

@router.get("/api/get_repos")
def get_repos():
    return data.repos


@router.get("/api/get_code_stats")
def get_code_stats():
    return data.code_stats


@router.get("/api/get_age")
def get_age():
    today = datetime.today()
    age = today.year - config.birthdate.year - (
            (today.month, today.day) < (config.birthdate.month, config.birthdate.day))
    return declension(age, 'год', 'года', 'лет')

