#/opt/docker/dev/service_finder/backend/app/api/v1/endpoints/users.py
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.exc import SQLAlchemyError
from typing import Dict, Any

from app.api.deps import get_db, get_current_user
from app.schemas.user import UserResponse, UserUpdate, ActiveOrganizationUpdate, UserWithTokenResponse
from app.models.identity import User
from app.services.trust_engine import TrustEngine
from app.core.security import create_tokens, DEFAULT_RANK_MAP
from app.core.config import settings

router = APIRouter()
trust_engine = TrustEngine()

@router.get("/me", response_model=UserResponse)
async def read_users_me(
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    """Visszaadja a bejelentkezett felhasználó profilját"""
    from sqlalchemy import select, or_
    from app.models.marketplace.organization import Organization, OrganizationMember
    from app.models.marketplace.organization import OrgUserRole
    
    # Determine active organization ID
    active_org_id = None
    
    # If user already has a scope_id, use it
    if current_user.scope_id is not None:
        try:
            active_org_id = int(current_user.scope_id)
        except (ValueError, TypeError):
            active_org_id = None
    
    # If still no active org ID, try to find user's primary organization
    if active_org_id is None:
        # 1. Check if user is a member of any organization with ADMIN/OWNER role
        stmt = select(OrganizationMember.organization_id).where(
            OrganizationMember.user_id == current_user.id,
            or_(
                OrganizationMember.role == OrgUserRole.ADMIN,
                OrganizationMember.role == OrgUserRole.OWNER
            )
        ).limit(1)
        
        result = await db.execute(stmt)
        org_member_row = result.first()
        
        if org_member_row:
            active_org_id = org_member_row[0]
        else:
            # 2. Check if user owns any organization (owner_id matches user.id)
            stmt = select(Organization.id).where(
                Organization.owner_id == current_user.id
            ).limit(1)
            result = await db.execute(stmt)
            org_owner_row = result.first()
            
            if org_owner_row:
                active_org_id = org_owner_row[0]
            else:
                # 3. Fallback: get first organization they're a member of
                stmt = select(OrganizationMember.organization_id).where(
                    OrganizationMember.user_id == current_user.id
                ).limit(1)
                result = await db.execute(stmt)
                org_row = result.first()
                
                active_org_id = org_row[0] if org_row else None
    
    # Create a response dictionary with the active_organization_id
    # Get first_name and last_name from person relation if available
    person = current_user.person
    # Safe extraction with fallback to empty string
    first_name = ""
    last_name = ""
    if person:
        first_name = getattr(person, 'first_name', '')
        last_name = getattr(person, 'last_name', '')
    
    response_data = {
        "id": current_user.id,
        "email": current_user.email,
        "first_name": first_name,
        "last_name": last_name,
        "is_active": current_user.is_active,
        "region_code": current_user.region_code,
        "person_id": current_user.person_id,
        "role": current_user.role.value if hasattr(current_user.role, 'value') else str(current_user.role),
        "subscription_plan": current_user.subscription_plan,
        "scope_level": current_user.scope_level or "individual",
        "scope_id": str(active_org_id) if active_org_id else None,
        "ui_mode": current_user.ui_mode or "personal",
        "active_organization_id": active_org_id
    }
    
    return UserResponse.model_validate(response_data)

@router.get("/me/trust")
async def get_user_trust(
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user),
    force_recalculate: bool = False,
) -> Dict[str, Any]:
    """
    Visszaadja a felhasználó Gondos Gazda Index (Trust Score) értékét.
    
    A számítás dinamikusan betölti a paramétereket a SystemParameter rendszerből
    (Global/Country/Region/User hierarchia).
    
    Paraméterek:
    - force_recalculate: Ha True, akkor újraszámolja a trust score-t
      (alapértelmezetten cache-elt értéket ad vissza, ha kevesebb mint 24 órája számoltuk)
    """
    trust_data = await trust_engine.calculate_user_trust(
        db=db,
        user_id=current_user.id,
        force_recalculate=force_recalculate
    )
    return trust_data


@router.patch("/me/preferences", response_model=UserResponse)
async def update_user_preferences(
    update_data: UserUpdate,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    """
    Update user preferences (ui_mode, preferred_language, etc.)
    """
    # Filter out None values
    update_dict = update_data.dict(exclude_unset=True)
    if not update_dict:
        raise HTTPException(status_code=400, detail="No fields to update")
    
    # Validate ui_mode if present
    if "ui_mode" in update_dict:
        if update_dict["ui_mode"] not in ["personal", "fleet"]:
            raise HTTPException(status_code=422, detail="ui_mode must be 'personal' or 'fleet'")
    
    # Update user fields
    for field, value in update_dict.items():
        if hasattr(current_user, field):
            setattr(current_user, field, value)
        else:
            raise HTTPException(status_code=400, detail=f"Invalid field: {field}")
    
    try:
        await db.commit()
        await db.refresh(current_user)
    except SQLAlchemyError as e:
        await db.rollback()
        raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
    
    # Return the Pydantic model instead of raw SQLAlchemy object
    return UserResponse.model_validate(current_user)


@router.patch("/me/active-organization", response_model=UserWithTokenResponse)
async def update_active_organization(
    update_data: ActiveOrganizationUpdate,
    db: AsyncSession = Depends(get_db),
    current_user: User = Depends(get_current_user),
):
    """
    Update the user's active organization (scope_id).
    
    Accepts an organization_id (UUID/string) or None to revert to personal mode.
    Returns a new JWT token with updated scope_id in the payload.
    """
    # Extract organization_id from request
    org_id = update_data.organization_id
    
    # Validate that the user has access to this organization if org_id is provided
    if org_id is not None:
        from sqlalchemy import select
        from app.models.marketplace.organization import OrganizationMember
        
        # Check if user is a member of the organization
        stmt = select(OrganizationMember).where(
            OrganizationMember.organization_id == org_id,
            OrganizationMember.user_id == current_user.id
        )
        result = await db.execute(stmt)
        member = result.scalar_one_or_none()
        
        if not member:
            raise HTTPException(
                status_code=403,
                detail="You are not a member of this organization"
            )
    
    # Update user's scope_id
    current_user.scope_id = org_id
    
    try:
        await db.commit()
        await db.refresh(current_user)
    except SQLAlchemyError as e:
        await db.rollback()
        raise HTTPException(status_code=500, detail=f"Database error: {str(e)}")
    
    # Generate new JWT token with updated scope_id
    role_key = current_user.role.value.upper()
    token_payload = {
        "sub": str(current_user.id),
        "role": role_key,
        "rank": DEFAULT_RANK_MAP.get(role_key, "user"),
        "scope_level": "organization" if org_id else "personal",
        "scope_id": org_id,
        "person_id": str(current_user.person_id) if current_user.person_id else None,
    }
    
    access_token, _ = create_tokens(data=token_payload)
    
    # Return user data with new token
    return UserWithTokenResponse(
        user=UserResponse.model_validate(current_user),
        access_token=access_token,
        token_type="bearer"
    )
