"""
Sales sequencer integrations (Instantly, Smartlead, Apollo, Outreach).

Provides a unified interface to push contacts to sequencers and receive engagement events.
Lazy-loaded to avoid impacting CLI startup performance.
"""

from __future__ import annotations

from typing import Optional


class SequencerError(RuntimeError):
    """Base exception for sequencer operations."""

    pass


class SequencerAuthError(SequencerError):
    """Authentication failed with sequencer API."""

    pass


class SequencerRateLimitError(SequencerError):
    """Rate limit exceeded on sequencer API."""

    def __init__(self, message: str, retry_after: Optional[float] = None):
        super().__init__(message)
        self.retry_after = retry_after


class SequencerValidationError(SequencerError):
    """Input validation failed (e.g., missing required fields)."""

    pass


# Lazy imports to avoid loading HTTP clients on --help
def get_provider(name: str, api_key: str, **kwargs):
    """
    Factory function to get a sequencer provider by name.

    Args:
        name: Provider name ('instantly', 'smartlead', 'apollo', or 'outreach')
        api_key: API key for the provider
        **kwargs: Additional provider-specific options

    Returns:
        SequencerProvider instance

    Raises:
        ValueError: If provider name is not recognized
    """
    if name == "instantly":
        from g_gremlin.sequencer.instantly import InstantlyProvider

        return InstantlyProvider(api_key=api_key, **kwargs)
    elif name == "smartlead":
        from g_gremlin.sequencer.smartlead import SmartleadProvider

        return SmartleadProvider(api_key=api_key, **kwargs)
    elif name == "apollo":
        from g_gremlin.sequencer.apollo import ApolloSequencerProvider

        return ApolloSequencerProvider(api_key=api_key, **kwargs)
    elif name == "outreach":
        from g_gremlin.sequencer.outreach import OutreachSequencerProvider

        return OutreachSequencerProvider(api_key=api_key, **kwargs)
    else:
        raise ValueError(
            "Unknown sequencer provider: "
            f"{name}. Valid options: instantly, smartlead, apollo, outreach"
        )


__all__ = [
    "SequencerError",
    "SequencerAuthError",
    "SequencerRateLimitError",
    "SequencerValidationError",
    "get_provider",
]
