from __future__ import annotations

from typing import Optional

from core.config import logger
from core.profile_sync_service import ProfileSyncService
from core.tenant_manager import TenantManager


class ProfileBootstrapService:
    def __init__(
        self,
        db,
        tenant_manager: Optional[TenantManager] = None,
        request_timeout_seconds: int = 20,
    ) -> None:
        self.db = db
        self.tenant_manager = tenant_manager or TenantManager()
        self.service = ProfileSyncService(
            db,
            tenant_manager=self.tenant_manager,
            request_timeout_seconds=request_timeout_seconds,
        )

    def run_if_needed(self) -> bool:
        result = self.service.run_profile_reconciliation()
        if result:
            try:
                self.tenant_manager.mark_profile_sync_bootstrap_completed(True)
            except Exception as exc:
                logger.warning("Profile bootstrap completion state could not be recorded: %s", exc)
        return result




