"""
Главный файл FastAPI приложения
"""
import os
import multiprocessing
import uvicorn
import logging
from fastapi import FastAPI

# Настройка логирования
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(name)s:%(lineno)d - %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)

# Настройка логгера для модуля
logger = logging.getLogger(__name__)

# Импортируем парсеры и обновляем PARSERS
from adapters.parsers import SvodkaPMParser, SvodkaCAParser, MonitoringFuelParser, MonitoringTarParser, SvodkaRepairCAParser, StatusesRepairCAParser, OperSpravkaTechPosParser
from core.services import PARSERS

# Обновляем словарь парсеров
PARSERS.update({
    'svodka_pm': SvodkaPMParser,
    'svodka_ca': SvodkaCAParser,
    'monitoring_fuel': MonitoringFuelParser,
    'monitoring_tar': MonitoringTarParser,
    'svodka_repair_ca': SvodkaRepairCAParser,
    'statuses_repair_ca': StatusesRepairCAParser,
    'oper_spravka_tech_pos': OperSpravkaTechPosParser,
})

# Создаем FastAPI приложение
app = FastAPI(
    title="Svodka Parser API",
    description="API для парсинга различных типов отчетов",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# Подключаем роутеры
from app.endpoints import (
    common, system, 
    svodka_pm, svodka_ca, monitoring_fuel,
    svodka_repair_ca, statuses_repair_ca, monitoring_tar, oper_spravka_tech_pos
)

app.include_router(common.router)
app.include_router(system.router)
app.include_router(svodka_pm.router)
app.include_router(svodka_ca.router)
app.include_router(monitoring_fuel.router)
app.include_router(svodka_repair_ca.router)
app.include_router(statuses_repair_ca.router)
app.include_router(monitoring_tar.router)
app.include_router(oper_spravka_tech_pos.router)


if __name__ == "__main__":
    # Настройка для запуска в продакшене
    workers = multiprocessing.cpu_count()
    uvicorn.run(
        "app.main:app",
        host="0.0.0.0",
        port=8000,
        workers=workers,
        reload=False
    )