# app/modules/marketplace/services/platform_signup_service.py
"""
OMS-specific signup extensions.

The core signup service has moved to app.modules.billing.services.signup_service.
This file retains OMS-specific logic (Letzshop store claiming) and provides
backwards-compatible re-exports.
"""

from __future__ import annotations

import logging

from sqlalchemy.orm import Session

from app.exceptions import ConflictException
from app.modules.billing.services.signup_service import (
    AccountCreationResult,
    SignupCompletionResult,
    SignupService,
    SignupSessionData,
    StoreCreationResult,
    signup_service,
)

logger = logging.getLogger(__name__)


class OmsSignupService:
    """OMS-specific signup extensions (Letzshop store claiming)."""

    def __init__(self, base_service: SignupService):
        self._base = base_service

    def check_store_claimed(self, db: Session, letzshop_slug: str) -> bool:
        """Check if a Letzshop store is already claimed."""
        from app.modules.tenancy.services.store_service import store_service

        return store_service.is_letzshop_slug_claimed(db, letzshop_slug)

    def claim_store(
        self,
        db: Session,
        session_id: str,
        letzshop_slug: str,
        letzshop_store_id: str | None = None,
    ) -> str:
        """
        Claim a Letzshop store for OMS signup.

        Args:
            db: Database session
            session_id: Signup session ID
            letzshop_slug: Letzshop store slug
            letzshop_store_id: Optional Letzshop store ID

        Returns:
            Generated store name

        Raises:
            ResourceNotFoundException: If session not found
            ConflictException: If store already claimed
        """
        self._base.get_session_or_raise(session_id)

        if self.check_store_claimed(db, letzshop_slug):
            raise ConflictException(
                message="This Letzshop store is already claimed",
            )

        store_name = letzshop_slug.replace("-", " ").title()

        self._base.update_session(session_id, {
            "letzshop_slug": letzshop_slug,
            "letzshop_store_id": letzshop_store_id,
            "store_name": store_name,
            "step": "store_claimed",
        })

        logger.info(f"Claimed store {letzshop_slug} for session {session_id}")
        return store_name


# Singleton
oms_signup_service = OmsSignupService(signup_service)

# Re-exports for backwards compatibility
PlatformSignupService = SignupService
platform_signup_service = signup_service

__all__ = [
    "OmsSignupService",
    "oms_signup_service",
    "PlatformSignupService",
    "platform_signup_service",
    "AccountCreationResult",
    "SignupCompletionResult",
    "SignupSessionData",
    "StoreCreationResult",
]
