# app/modules/tenancy/routes/api/store.py
"""
Tenancy module store API routes.

Aggregates all store tenancy routes:
- /info/{store_code} - Public store info lookup
- /auth/* - Store authentication (login, logout, /me)
- /profile/* - Store profile management
- /team/* - Team member management, roles, permissions

The tenancy module owns identity and organizational hierarchy.
"""

import logging

from fastapi import APIRouter, Depends, Path
from sqlalchemy.orm import Session

from app.core.database import get_db
from app.modules.tenancy.schemas.store import StoreDetailResponse
from app.modules.tenancy.services.store_service import store_service  # mod-004

router = APIRouter()
logger = logging.getLogger(__name__)


@router.get("/info/{store_code}", response_model=StoreDetailResponse)
def get_store_info(
    store_code: str = Path(..., description="Store code"),
    db: Session = Depends(get_db),
):
    """
    Get public store information by store code.

    This endpoint is used by the store login page to display store info.
    No authentication required - this is public information.

    **Use Case:**
    - Store login page loads store info to display branding
    - Shows store name, description, logo, etc.

    **Returns only active stores** to prevent access to disabled accounts.

    Args:
        store_code: The store's unique code (e.g., 'ORION')
        db: Database session

    Returns:
        StoreResponse: Public store information

    Raises:
        StoreNotFoundException (404): Store not found or inactive
    """
    logger.info(f"Public store info request: {store_code}")

    store = store_service.get_active_store_by_code(db, store_code)

    logger.info(f"Store info retrieved: {store.name} ({store.store_code})")

    return StoreDetailResponse(
        # Store fields
        id=store.id,
        store_code=store.store_code,
        subdomain=store.subdomain,
        name=store.name,
        description=store.description,
        merchant_id=store.merchant_id,
        letzshop_csv_url_fr=store.letzshop_csv_url_fr,
        letzshop_csv_url_en=store.letzshop_csv_url_en,
        letzshop_csv_url_de=store.letzshop_csv_url_de,
        is_active=store.is_active,
        is_verified=store.is_verified,
        created_at=store.created_at,
        updated_at=store.updated_at,
        # Merchant info
        merchant_name=store.merchant.name,
        merchant_contact_email=store.merchant.contact_email,
        merchant_contact_phone=store.merchant.contact_phone,
        merchant_website=store.merchant.website,
        # Owner details (from merchant)
        owner_user_id=store.merchant.owner_user_id,
        owner_email=store.merchant.owner.email,
        owner_username=store.merchant.owner.username,
    )


# ============================================================================
# Aggregate Sub-Routers
# ============================================================================
# Include all tenancy store routes (auth, profile, team)

from .store_auth import store_auth_router
from .store_profile import store_profile_router
from .store_team import store_team_router
from .user_account import store_account_router

router.include_router(store_account_router, tags=["store-account"])
router.include_router(store_auth_router, tags=["store-auth"])
router.include_router(store_profile_router, tags=["store-profile"])
router.include_router(store_team_router, tags=["store-team"])
