"""Nexus — Sync Service (Log queries + audit helper)

W3: Migrated from paramiko pool to asyncssh pool.
Dead code removed: batch_push, _push_single, _add_retry, _rsync_flags,
_parse_rsync_transferred — all had zero callers. The primary sync path
is SyncEngineV2 (sync_engine_v2.py) invoked via schedule_runner.
"""

import logging
from typing import List

from server.domain.models import SyncLog, AuditLog
from server.domain.repositories import (
    ServerRepository, SyncLogRepository, AuditLogRepository,
    PushRetryJobRepository,
)

logger = logging.getLogger("nexus.sync_service")


class SyncService:
    """Business logic for sync log queries + audit helper"""

    def __init__(
        self,
        server_repo: ServerRepository,
        sync_log_repo: SyncLogRepository,
        audit_repo: AuditLogRepository,
        retry_repo: PushRetryJobRepository,
    ):
        self.server_repo = server_repo
        self.sync_log_repo = sync_log_repo
        self.audit_repo = audit_repo
        self.retry_repo = retry_repo

    async def get_sync_logs(self, server_id: int, limit: int = 50) -> List[SyncLog]:
        return await self.sync_log_repo.get_by_server_id(server_id, limit)

    async def _audit(self, action: str, target_type: str, target_id: int, detail: str):
        if not self.audit_repo:
            return
        log = AuditLog(action=action, target_type=target_type, target_id=target_id, detail=detail)
        await self.audit_repo.create(log)
