# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/system_parameters.py
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, update
from typing import List, Optional

from app.api.deps import get_db, get_current_user
from app.schemas.system import (
    SystemParameterResponse,
    SystemParameterUpdate,
    SystemParameterCreate,
)
from app.models.system import SystemParameter, ParameterScope
from app.models.identity import UserRole

router = APIRouter()


@router.get("/", response_model=List[SystemParameterResponse])
async def list_system_parameters(
    db: AsyncSession = Depends(get_db),
    scope_level: Optional[ParameterScope] = Query(None, description="Scope szint (global, country, region, user)"),
    scope_id: Optional[str] = Query(None, description="Scope azonosító (pl. 'HU', 'budapest', user_id)"),
    is_active: Optional[bool] = Query(True, description="Csak aktív paraméterek"),
):
    """
    Listázza az összes aktív (vagy opcionálisan inaktív) rendszerparamétert.
    Szűrhető scope_level és scope_id alapján.
    """
    query = select(SystemParameter)
    
    if scope_level is not None:
        query = query.where(SystemParameter.scope_level == scope_level)
    if scope_id is not None:
        query = query.where(SystemParameter.scope_id == scope_id)
    if is_active is not None:
        query = query.where(SystemParameter.is_active == is_active)
    
    result = await db.execute(query)
    parameters = result.scalars().all()
    return parameters


@router.get("/{key}", response_model=SystemParameterResponse)
async def get_system_parameter(
    key: str,
    db: AsyncSession = Depends(get_db),
    scope_level: ParameterScope = Query("global", description="Scope szint (alapértelmezett: global)"),
    scope_id: Optional[str] = Query(None, description="Scope azonosító"),
):
    """
    Visszaad egy konkrét paramétert a key és scope_level (és opcionálisan scope_id) alapján.
    """
    query = select(SystemParameter).where(
        SystemParameter.key == key,
        SystemParameter.scope_level == scope_level,
    )
    if scope_id is not None:
        query = query.where(SystemParameter.scope_id == scope_id)
    else:
        query = query.where(SystemParameter.scope_id.is_(None))
    
    result = await db.execute(query)
    parameter = result.scalar_one_or_none()
    
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"System parameter not found with key='{key}', scope_level='{scope_level}', scope_id='{scope_id}'"
        )
    return parameter


@router.put("/{key}", response_model=SystemParameterResponse)
async def update_system_parameter(
    key: str,
    param_in: SystemParameterUpdate,
    db: AsyncSession = Depends(get_db),
    current_user=Depends(get_current_user),
    scope_level: ParameterScope = Query("global", description="Scope szint (alapértelmezett: global)"),
    scope_id: Optional[str] = Query(None, description="Scope azonosító"),
):
    """
    Módosítja egy létező paraméter value (JSONB) vagy is_active mezőjét (Admin funkció).
    Csak superadmin vagy admin jogosultságú felhasználók használhatják.
    """
    # Jogosultság ellenőrzése
    if current_user.role not in (UserRole.superadmin, UserRole.admin):
        raise HTTPException(
            status_code=403,
            detail="Insufficient permissions. Only superadmin or admin can update system parameters."
        )
    
    # Paraméter keresése
    query = select(SystemParameter).where(
        SystemParameter.key == key,
        SystemParameter.scope_level == scope_level,
    )
    if scope_id is not None:
        query = query.where(SystemParameter.scope_id == scope_id)
    else:
        query = query.where(SystemParameter.scope_id.is_(None))
    
    result = await db.execute(query)
    parameter = result.scalar_one_or_none()
    
    if not parameter:
        raise HTTPException(
            status_code=404,
            detail=f"System parameter not found with key='{key}', scope_level='{scope_level}', scope_id='{scope_id}'"
        )
    
    # Frissítés
    update_data = {}
    if param_in.description is not None:
        update_data["description"] = param_in.description
    if param_in.value is not None:
        update_data["value"] = param_in.value
    if param_in.is_active is not None:
        update_data["is_active"] = param_in.is_active
    
    if update_data:
        stmt = (
            update(SystemParameter)
            .where(SystemParameter.id == parameter.id)
            .values(**update_data)
        )
        await db.execute(stmt)
        await db.commit()
        await db.refresh(parameter)
    
    return parameter