from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from typing import Optional, List
from utils.utils import get_db
from . import handler
from .schema import (
    SystemconfigurationCreate,
    SystemconfigurationUpdate,
    SystemconfigurationResponse,
    ConfigDataType,
)

router = APIRouter(prefix="/system-configurations", tags=["System Configuration"])


@router.post(
    "/",
    response_model=SystemconfigurationResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create System Configuration",
    description="Creates a new system configuration record. Validates that the configuration key is unique. Returns 409 if the key already exists. Returns 400 if the modified_by_user_id references a non-existent user.",
)
def create_systemconfiguration_route(
    data: SystemconfigurationCreate,
    db: Session = Depends(get_db),
):
    return handler.create_systemconfiguration(db, data)


@router.get(
    "/",
    response_model=List[SystemconfigurationResponse],
    summary="List System Configurations",
    description="Returns a paginated list of system configurations. Supports optional search filter on key and description fields, and optional data_type filter. Results are ordered by key in ascending order.",
)
def list_systemconfigurations_route(
    limit: int = Query(20, ge=1, le=100),
    offset: int = Query(0, ge=0),
    search: Optional[str] = None,
    data_type: Optional[ConfigDataType] = Query(None),
    db: Session = Depends(get_db),
):
    return handler.list_systemconfigurations(db, limit, offset, search, data_type.value if data_type else None)


@router.get(
    "/by-key/{key}",
    response_model=SystemconfigurationResponse,
    summary="Get System Configuration By Key",
    description="Retrieves a system configuration by its unique key. Returns 404 if the configuration key does not exist.",
)
def get_systemconfiguration_by_key_route(
    key: str,
    db: Session = Depends(get_db),
):
    return handler.get_systemconfiguration_by_key(db, key)


@router.get(
    "/{id}",
    response_model=SystemconfigurationResponse,
    summary="Get System Configuration",
    description="Retrieves a system configuration by its unique identifier. Returns 404 if the configuration does not exist.",
)
def get_systemconfiguration_route(
    id: str,
    db: Session = Depends(get_db),
):
    return handler.get_systemconfiguration(db, id)


@router.get(
    "/{id}/details",
    response_model=SystemconfigurationResponse,
    summary="Get System Configuration With Details",
    description="Retrieves a system configuration with related user details (modified_by_user). Returns 404 if the configuration does not exist.",
)
def get_systemconfiguration_with_details_route(
    id: str,
    db: Session = Depends(get_db),
):
    return handler.get_systemconfiguration_with_details(db, id)


@router.put(
    "/{id}",
    response_model=SystemconfigurationResponse,
    summary="Update System Configuration",
    description="Updates an existing system configuration. Validates that the new key (if provided) is unique. Returns 404 if the configuration does not exist. Returns 409 if the new key conflicts with an existing configuration. Returns 400 if the modified_by_user_id references a non-existent user.",
)
def update_systemconfiguration_route(
    id: str,
    data: SystemconfigurationUpdate,
    db: Session = Depends(get_db),
):
    return handler.update_systemconfiguration(db, id, data)


@router.delete(
    "/{id}",
    status_code=status.HTTP_200_OK,
    summary="Delete System Configuration",
    description="Deletes a system configuration by its unique identifier. Returns 404 if the configuration does not exist.",
)
def delete_systemconfiguration_route(
    id: str,
    db: Session = Depends(get_db),
):
    return handler.delete_systemconfiguration(db, id)