from pathlib import Path
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import FileResponse
from app.api.deps import get_current_user
from app.core.config import settings

router = APIRouter(prefix='/api/v028', tags=['V028 Native Installation'])

@router.get('/native-status')
def native_status(user=Depends(get_current_user)):
    return {
        'version': 'V028',
        'native_install': settings.native_install,
        'database': settings.metadata_database_url.split(':',1)[0],
        'storage_backend': settings.storage_backend,
        'storage_path': settings.local_storage_path if settings.storage_backend == 'local' else settings.s3_bucket,
        'redis_optional': settings.redis_optional,
        'docker_required': False,
    }

@router.get('/local-files/{object_key:path}')
def local_file(object_key: str, user=Depends(get_current_user)):
    if settings.storage_backend != 'local': raise HTTPException(404, 'Armazenamento local não está ativo')
    root = Path(settings.local_storage_path).resolve()
    target = (root / object_key.replace('..','_').lstrip('/')).resolve()
    if root not in target.parents or not target.is_file(): raise HTTPException(404, 'Arquivo não encontrado')
    return FileResponse(target)
