{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://schema.dpksystem.cloud/Web/tenant-config.schema.json",
  "title": "USP Tenant Configuration",
  "description": "Editable configuration per tenant — routes, features, module settings, translations, logging, UI",
  "type": "object",
  "additionalProperties": false,
  "required": ["tenantId", "schemaVersion"],

  "definitions": {
    "LogLevel": {
      "type": "string",
      "enum": ["Trace", "Debug", "Info", "Warning", "Error", "Fatal", "None"]
    },

    "RouteKey": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]+(?:-[a-z0-9]+)*$",
      "description": "Route key w kebab-case (np. usp-vhbook-select-vehicle)"
    },

    "GroupName": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9-]*$",
      "description": "Group name w kebab-case (np. employee-portal, business-intelligence)"
    },

    "RouteOverride": {
      "type": "object",
      "additionalProperties": false,
      "description": "Per-route field overrides — nadpisuje wartości z BASE_ROUTES",
      "properties": {
        "enabled": {
          "type": "boolean",
          "description": "Wyłączenie route bez usuwania z groupy (soft disable). Preferowane: użyj disabledRoutes."
        },
        "path": {
          "type": "string",
          "pattern": "^/",
          "description": "Custom URL path dla tego route (np. '/dashboard-kml')"
        },
        "component": {
          "type": "string",
          "description": "Nazwa custom komponentu - musi być w bundle jako view"
        },
        "auth": {
          "type": "array",
          "items": { "type": "string" },
          "uniqueItems": true,
          "description": "Wymagane role dla dostępu (np. ['manager', 'supervisor'])"
        },
        "devices": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["desktop", "tablet", "mobile"]
          },
          "uniqueItems": true,
          "description": "Dozwolone urządzenia"
        },
        "orientation": {
          "type": "string",
          "enum": ["portrait", "landscape", "any"],
          "description": "Dozwolona orientacja ekranu"
        },
        "props": {
          "type": "object",
          "description": "Extra props przekazane do komponentu"
        }
      }
    },

    "EmployeePortalModule": {
      "type": "object",
      "additionalProperties": false,
      "description": "Konfiguracja modułu Employee Portal",
      "properties": {
        "selectDeclarationTypeFirstMode": {
          "type": "boolean",
          "default": false,
          "description": "Tryb 'wybierz typ deklaracji jako pierwszy krok' w formularzu urlopu"
        },
        "preferenceSimplePlannerDayEnlarged": {
          "type": "boolean",
          "default": false,
          "description": "Powiększone dni w prostym planerze preferencji"
        },
        "definitionSelectionListGroupedByType": {
          "type": "boolean",
          "default": false,
          "description": "Grupowanie listy definicji po typie w selektorze"
        }
      }
    },

    "AbsenceModule": {
      "type": "object",
      "additionalProperties": false,
      "description": "Konfiguracja modułu Absence (nieobecności)",
      "properties": {
        "dayLimitAvailability": {
          "type": "object",
          "additionalProperties": false,
          "required": ["medium", "hard"],
          "description": "Progi ratio dla oznaczania dostępności dnia (0-1)",
          "properties": {
            "medium": {
              "type": "number",
              "minimum": 0,
              "maximum": 1,
              "description": "Ratio powyżej którego dzień jest 'medium busy' (np. 0.6 = 60%)"
            },
            "hard": {
              "type": "number",
              "minimum": 0,
              "maximum": 1,
              "description": "Ratio powyżej którego dzień jest 'hard busy' (np. 0.8 = 80%)"
            }
          }
        }
      }
    },

    "VhbookModule": {
      "type": "object",
      "additionalProperties": false,
      "description": "Konfiguracja modułu Vehicle Log Book",
      "properties": {
        "syncIntervalMinutes": {
          "type": "integer",
          "minimum": 1,
          "maximum": 1440,
          "default": 15,
          "description": "Interwał synchronizacji work diary (w minutach)"
        },
        "allowManualIssueCategoryCreation": {
          "type": "boolean",
          "default": true,
          "description": "Czy user może tworzyć custom kategorie issue"
        }
      }
    }
  },

  "properties": {
    "$schema": {
      "type": "string",
      "format": "uri",
      "description": "Link do JSON Schema — dla VS Code autocomplete/walidacji"
    },

    "tenantId": {
      "type": "string",
      "pattern": "^[A-Z][A-Z0-9_]*$",
      "minLength": 2,
      "maxLength": 32,
      "description": "Klucz tenanta - musi się zgadzać z tenantId w identity file",
      "examples": ["KML", "MPK_LODZ", "DEFAULT"]
    },

    "schemaVersion": {
      "type": "integer",
      "const": 2,
      "description": "Wersja tego JSON Schema. Obecnie 2. Bump przy breaking change."
    },

    "notes": {
      "type": "string",
      "maxLength": 500,
      "description": "Opcjonalne notatki dla dev/admin (nie używane w runtime)"
    },

    "bundleVersionMin": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$",
      "description": "Minimalna wersja bundle wymagana do obsługi tego config. Format SemVer (major.minor.patch)."
    },

    "translation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["productId", "dxProductId"],
      "description": "Konfiguracja translations (product ID w systemie tłumaczeń)",
      "properties": {
        "productId": {
          "type": "string",
          "format": "uuid",
          "description": "Product ID dla USP translations",
          "examples": ["34a128ea-51c6-4b57-afe5-d06fe2ab2392"]
        },
        "dxProductId": {
          "type": "string",
          "format": "uuid",
          "description": "Product ID dla DevExtreme translations",
          "examples": ["73142ed6-b711-4615-8c1b-8350a1744812"]
        },
        "clientId": {
          "type": ["string", "null"],
          "format": "uuid",
          "default": null,
          "description": "Optional client-specific translation override ID"
        }
      }
    },

    "logging": {
      "type": "object",
      "additionalProperties": false,
      "description": "Konfiguracja loggingu",
      "properties": {
        "includeDefault": {
          "type": "boolean",
          "default": true,
          "description": "Czy zawierać logi z domyślnej konsoli"
        },
        "includeWebVitals": {
          "type": "boolean",
          "default": false,
          "description": "Czy zawierać logi wydajności (Web Vitals)"
        },
        "console": {
          "type": "array",
          "items": { "$ref": "#/definitions/LogLevel" },
          "uniqueItems": true,
          "default": ["Error", "Fatal"],
          "description": "Poziomy logów wysyłane do console. Pusta lista = wyłączone."
        },
        "logCenter": {
          "type": "array",
          "items": { "$ref": "#/definitions/LogLevel" },
          "uniqueItems": true,
          "default": [],
          "description": "Poziomy logów wysyłane do LogCenter (serwer). Pusta lista = wyłączone."
        }
      }
    },

    "ui": {
      "type": "object",
      "additionalProperties": false,
      "description": "Widoczność globalnych elementów UI",
      "properties": {
        "menu": {
          "type": "boolean",
          "default": true,
          "description": "Czy pokazywać menu głównego (false np. dla embedded mode)"
        },
        "viewTitle": {
          "type": "boolean",
          "default": true,
          "description": "Czy pokazywać tytuł widoku"
        },
        "notifications": {
          "type": "boolean",
          "default": true,
          "description": "Czy pokazywać widget notyfikacji"
        }
      }
    },

    "modules": {
      "type": "object",
      "additionalProperties": { "type": "object" },
      "description": "Konfiguracja per moduł. Known moduły są walidowane, nowe (nie w schema) przechodzą jako any object (forward compat).",
      "properties": {
        "employeePortal": { "$ref": "#/definitions/EmployeePortalModule" },
        "absence": { "$ref": "#/definitions/AbsenceModule" },
        "vhbook": { "$ref": "#/definitions/VhbookModule" }
      }
    },

    "features": {
      "type": "object",
      "additionalProperties": { "type": "boolean" },
      "description": "Feature flags — arbitrary key: boolean. Nieznane flagi na frontendzie zwracają false.",
      "examples": [
        {
          "advancedFilters": true,
          "experimentalDashboard": false,
          "kmlCustomBranding": true
        }
      ]
    },

    "routes": {
      "type": "object",
      "additionalProperties": false,
      "description": "Konfiguracja routingu — które routes/grupy są aktywne, per-route overrides",
      "properties": {
        "enabledGroups": {
          "type": "array",
          "items": { "$ref": "#/definitions/GroupName" },
          "uniqueItems": true,
          "default": [],
          "description": "Grupy routes do aktywacji (allowlist). Grupa 'core' aktywna zawsze automatycznie.",
          "examples": [["employee-portal", "vhbook", "tenant-admin"]]
        },
        "enabledRoutes": {
          "type": "array",
          "items": { "$ref": "#/definitions/RouteKey" },
          "uniqueItems": true,
          "default": [],
          "description": "Pojedyncze routes do aktywacji spoza wybranych grup (fine-grained addition)"
        },
        "disabledRoutes": {
          "type": "array",
          "items": { "$ref": "#/definitions/RouteKey" },
          "uniqueItems": true,
          "default": [],
          "description": "Pojedyncze routes do wyłączenia z aktywowanych grup (fine-grained subtraction)"
        },
        "disabledGroups": {
          "type": "array",
          "items": { "$ref": "#/definitions/GroupName" },
          "uniqueItems": true,
          "default": [],
          "description": "Grupy do wyłączenia (rzadko potrzebne przy allowlist approach — użyj gdy naprawdę potrzeba)"
        },
        "overrides": {
          "type": "object",
          "additionalProperties": { "$ref": "#/definitions/RouteOverride" },
          "default": {},
          "description": "Per-route field overrides — key = route key, value = override object",
          "examples": [
            {
              "usp-general-home": { "path": "/dashboard-kml" },
              "usp-ep-schedule-time-sheet": { "auth": ["manager"] }
            }
          ]
        }
      }
    }
  }
}
