# app/modules/tenancy/routes/api/admin_store_subdomains.py
"""
Admin endpoints for managing store custom subdomains (per-platform).

Each store can have a custom subdomain on each platform it belongs to.
For example, store "WizaTech" on the loyalty platform could have
custom_subdomain="wizatech-rewards" → wizatech-rewards.rewardflow.lu
"""

import logging

from fastapi import APIRouter, Body, Depends, Path
from pydantic import BaseModel, Field
from sqlalchemy.orm import Session

from app.api.deps import get_current_admin_api
from app.core.database import get_db
from app.modules.tenancy.schemas.auth import UserContext
from app.modules.tenancy.services.store_subdomain_service import store_subdomain_service

admin_store_subdomains_router = APIRouter(prefix="/stores")
logger = logging.getLogger(__name__)


# ── Schemas ──────────────────────────────────────────────────────────────────


class CustomSubdomainEntry(BaseModel):
    store_platform_id: int
    platform_id: int
    platform_code: str
    platform_name: str
    platform_domain: str | None
    custom_subdomain: str | None
    default_subdomain: str | None
    full_url: str | None
    default_url: str | None


class CustomSubdomainListResponse(BaseModel):
    subdomains: list[CustomSubdomainEntry]
    total: int


class SetCustomSubdomainRequest(BaseModel):
    subdomain: str = Field(
        ...,
        min_length=3,
        max_length=63,
        description="Custom subdomain (lowercase, alphanumeric + hyphens)",
    )


class CustomSubdomainUpdateResponse(BaseModel):
    message: str
    platform_id: int
    custom_subdomain: str | None


# ── Endpoints ────────────────────────────────────────────────────────────────


@admin_store_subdomains_router.get(
    "/{store_id}/custom-subdomains",
    response_model=CustomSubdomainListResponse,
)
def list_custom_subdomains(
    store_id: int = Path(..., description="Store ID", gt=0),
    db: Session = Depends(get_db),
    current_admin: UserContext = Depends(get_current_admin_api),
):
    """
    List all platform memberships with their custom subdomains.

    Returns one entry per active platform the store belongs to,
    showing the custom_subdomain (if set) and the default subdomain.
    """
    entries = store_subdomain_service.get_custom_subdomains(db, store_id)
    return CustomSubdomainListResponse(
        subdomains=[CustomSubdomainEntry(**e) for e in entries],
        total=len(entries),
    )


@admin_store_subdomains_router.put(
    "/{store_id}/custom-subdomains/{platform_id}",
    response_model=CustomSubdomainUpdateResponse,
)
def set_custom_subdomain(
    store_id: int = Path(..., description="Store ID", gt=0),
    platform_id: int = Path(..., description="Platform ID", gt=0),
    payload: SetCustomSubdomainRequest = Body(...),
    db: Session = Depends(get_db),
    current_admin: UserContext = Depends(get_current_admin_api),
):
    """
    Set or update the custom subdomain for a store on a specific platform.

    The subdomain must be unique on the platform (no other store can claim it).
    Format: lowercase alphanumeric + hyphens, 3-63 characters.
    """
    sp = store_subdomain_service.set_custom_subdomain(
        db, store_id, platform_id, payload.subdomain
    )
    db.commit()

    return CustomSubdomainUpdateResponse(
        message=f"Custom subdomain set to '{sp.custom_subdomain}'",
        platform_id=platform_id,
        custom_subdomain=sp.custom_subdomain,
    )


@admin_store_subdomains_router.delete(
    "/{store_id}/custom-subdomains/{platform_id}",
    response_model=CustomSubdomainUpdateResponse,
)
def clear_custom_subdomain(
    store_id: int = Path(..., description="Store ID", gt=0),
    platform_id: int = Path(..., description="Platform ID", gt=0),
    db: Session = Depends(get_db),
    current_admin: UserContext = Depends(get_current_admin_api),
):
    """
    Clear the custom subdomain for a store on a specific platform.

    The store will still be accessible via its default subdomain
    (Store.subdomain + platform domain).
    """
    store_subdomain_service.clear_custom_subdomain(db, store_id, platform_id)
    db.commit()

    return CustomSubdomainUpdateResponse(
        message="Custom subdomain cleared",
        platform_id=platform_id,
        custom_subdomain=None,
    )
