"""Configuration validation endpoints."""

import logging
import os
from typing import Dict, Any

from fastapi import APIRouter

from src.models.schemas import ResponseModel
from src.utils.errors import AppException, ErrorCode
from src.services.provider.validation import ConfigValidator, validate_all_configs

logger = logging.getLogger(__name__)
router = APIRouter()


@router.get("/config/validate", response_model=ResponseModel)
async def validate_configurations():
    """Validate all service configurations."""
    try:
        report = validate_all_configs()
        return ResponseModel(data=report)
    except Exception as e:
        logger.error(f"Failed to validate configurations: {e}")
        raise AppException(
            message=str(e),
            code=ErrorCode.INTERNAL_ERROR,
            status_code=500
        )


@router.post("/config/validate/service", response_model=ResponseModel)
async def validate_service_configuration(config: Dict[str, Any]):
    """Validate a single service configuration."""
    try:
        is_valid, errors = ConfigValidator.validate_config_deep(config)
        return ResponseModel(data={
            "valid": is_valid,
            "errors": errors,
            "config": config
        })
    except Exception as e:
        logger.error(f"Failed to validate service configuration: {e}")
        raise AppException(
            message=str(e),
            code=ErrorCode.INTERNAL_ERROR,
            status_code=500
        )


@router.get("/config/validate/file/{filename}", response_model=ResponseModel)
async def validate_config_file(filename: str):
    """Validate a specific configuration file."""
    try:
        src_dir = os.path.dirname(os.path.dirname(__file__))
        config_path = os.path.join(src_dir, "config", "services", filename)

        if not os.path.exists(config_path):
            raise AppException(
                message=f"Configuration file not found: {filename}",
                code=ErrorCode.NOT_FOUND,
                status_code=404
            )

        is_valid, errors = ConfigValidator.validate_config_file(config_path)
        return ResponseModel(data={
            "filename": filename,
            "valid": is_valid,
            "errors": errors
        })
    except AppException:
        raise
    except Exception as e:
        logger.error(f"Failed to validate config file: {e}")
        raise AppException(
            message=str(e),
            code=ErrorCode.INTERNAL_ERROR,
            status_code=500
        )
