# Web Application Settings Mechanism

This document describes the settings management system for the Superset Tools application.

## Overview

The settings mechanism allows users to configure multiple Superset environments and global application settings (like backup storage) via the web UI.

## Backend Architecture

### Data Models

Configuration is structured using Pydantic models in `backend/src/core/config_models.py`:

- `Environment`: Represents a Superset instance (URL, credentials). The `base_url` is automatically normalized to include the `/api/v1` suffix if missing.
- `GlobalSettings`: Global application parameters (e.g., `storage.root_path`).
- `AppConfig`: The root configuration object.

### Configuration Manager

The `ConfigManager` (`backend/src/core/config_manager.py`) handles:
- Persistence to `config.json`.
- CRUD operations for environments.
- Validation and logging.

### API Endpoints

The settings API is available at `/settings`:

- `GET /settings`: Retrieve all settings (passwords are masked).
- `PATCH /settings/global`: Update global settings.
- `GET /settings/environments`: List environments.
- `POST /settings/environments`: Add environment.
- `PUT /settings/environments/{id}`: Update environment.
- `DELETE /settings/environments/{id}`: Remove environment.
- `POST /settings/environments/{id}/test`: Test connection.

## Frontend Implementation

The settings page is located at `frontend/src/pages/Settings.svelte`. It provides forms for managing global settings and Superset environments.

## Integration

Existing plugins and utilities use the `ConfigManager` to fetch configuration:
- `superset_tool/utils/init_clients.py`: Dynamically initializes Superset clients from the configured environments.
- `BackupPlugin`: Uses the configured `storage.root_path` as the default storage location.
