import logging
from pathlib import Path
from pyrag3.gitea_api import GiteaAPI

# Hardcoded repository details as requested
REPO_URL = "https://f8e1300d871e905e27ce54c3455a3343104d9b04@git.client.guacamolebox.net/butterfly/ragdocs.git"

logger = logging.getLogger(__name__)

class RepoManager:
    def __init__(self, repo_url=REPO_URL):
        self.repo_url = repo_url
        self.api = GiteaAPI()

    def sync_repo(self):
        """Deprecated: We no longer clone or pull to save storage."""
        logger.info("Syncing repository metadata via API (skipping clone/pull)...")
        # In the new architecture, sync is handled by the RetrievalService 
        # listing files via API.
        pass

    def commit_and_push(self, file_paths, message="Added new documents"):
        """Upload new files directly to the remote repository via API."""
        logger.info(f"Uploading {len(file_paths)} files to repository via API...")
        uploaded_urls = []
        
        for file_path in file_paths:
            file_path = Path(file_path)
            # Use relative path from the designated "web_pages" root or similar
            # For simplicity, we'll use a standard remote folder
            remote_path = f"web_results/{file_path.name}"
            
            url = self.api.upload_file(file_path, remote_path, message=message)
            if url:
                uploaded_urls.append(url)
                logger.debug(f"Uploaded {file_path} to {url}")
            else:
                logger.error(f"Failed to upload {file_path}")
        
        return uploaded_urls

if __name__ == "__main__":
    # Quick test
    logging.basicConfig(level=logging.INFO)
    manager = RepoManager()
    manager.sync_repo()

if __name__ == "__main__":
    # Quick test
    logging.basicConfig(level=logging.INFO)
    manager = RepoManager()
    manager.sync_repo()
