# Example showing how to create a service using the standard interface
{
  config,
  lib,
  pkgs,
  ...
}:
with lib; let
  serviceInterface = import ../../lib/service-interface.nix {inherit lib;};

  cfg = config.homelab.services.grafana;
  homelabCfg = config.homelab;

  # Service-specific options beyond the standard interface
  grafanaServiceOptions = {
    admin = {
      user = mkOption {
        type = types.str;
        default = "admin";
        description = "Admin username";
      };

      passwordFile = mkOption {
        type = types.str;
        default = "admin";
        description = "Path to the Admin password file";
      };
    };

    datasources = {
      prometheus = {
        enable = mkOption {
          type = types.bool;
          default = true;
          description = "Enable Prometheus datasource";
        };

        url = mkOption {
          type = types.str;
          default = "http://127.0.0.1:9090";
          description = "Prometheus URL";
        };

        uid = mkOption {
          type = types.str;
          default = "prometheus";
          description = "Unique identifier for Prometheus datasource";
        };
      };

      loki = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = "Enable Loki datasource";
        };

        url = mkOption {
          type = types.str;
          default = "http://127.0.0.1:3100";
          description = "Loki URL";
        };

        uid = mkOption {
          type = types.str;
          default = "loki";
          description = "Unique identifier for Loki datasource";
        };
      };

      influxdb = {
        enable = mkOption {
          type = types.bool;
          default = false;
          description = "Enable InfluxDB datasource";
        };

        url = mkOption {
          type = types.str;
          default = "http://127.0.0.1:8086";
          description = "InfluxDB URL";
        };

        database = mkOption {
          type = types.str;
          default = "homelab";
          description = "InfluxDB database name";
        };

        tokenFile = mkOption {
          type = types.nullOr types.path;
          default = null;
          description = "Path to InfluxDB token file";
        };

        uid = mkOption {
          type = types.str;
          default = "influxdb";
          description = "Unique identifier for InfluxDB datasource";
        };
      };

      extra = mkOption {
        type = types.listOf types.attrs;
        default = [];
        description = "Additional data sources";
      };
    };

    plugins = mkOption {
      type = types.listOf types.package;
      default = [];
      description = "Grafana plugins to install";
    };
  };
in {
  options.homelab.services.grafana = serviceInterface.mkServiceInterface {
    serviceName = "grafana";
    defaultPort = 3000;
    defaultSubdomain = "grafana";
    monitoringPath = "/metrics";
    healthCheckPath = "/api/health";
    healthCheckConditions = [
      "[STATUS] == 200"
      "[BODY].database == ok"
      "[RESPONSE_TIME] < 2000"
    ];
    serviceOptions = grafanaServiceOptions;
  };

  config = serviceInterface.mkServiceConfig {
    inherit config cfg homelabCfg;
    serviceName = "grafana";

    extraMonitoringLabels = {
      component = "dashboard";
    };

    serviceConfig = {
      services.grafana = {
        enable = true;
        declarativePlugins = cfg.plugins;

        settings = {
          server = {
            http_port = cfg.port;
            http_addr = "0.0.0.0";
            root_url = "https://${cfg.proxy.subdomain}.${homelabCfg.externalDomain}";
          };

          security = {
            admin_user = cfg.admin.user;
            admin_password = "$__file{${cfg.admin.passwordFile}}";
          };
        };

        provision = {
          enable = true;
          datasources.settings = {
            datasources = let
              # Build datasource list
              datasources =
                []
                ++ optional cfg.datasources.prometheus.enable {
                  uid = cfg.datasources.prometheus.uid;
                  name = "Prometheus";
                  type = "prometheus";
                  url = cfg.datasources.prometheus.url;
                }
                ++ optional cfg.datasources.loki.enable {
                  uid = cfg.datasources.loki.uid;
                  name = "Loki";
                  type = "loki";
                  url = cfg.datasources.loki.url;
                }
                ++ optional cfg.datasources.influxdb.enable {
                  uid = cfg.datasources.influxdb.uid;
                  name = "InfluxDB";
                  type = "influxdb";
                  url = cfg.datasources.influxdb.url;
                  access = "proxy";
                  jsonData = {
                    dbName = cfg.datasources.influxdb.database;
                    httpHeaderName1 = "Authorization";
                  };
                  secureJsonData = mkIf (cfg.datasources.influxdb.tokenPath != null) {
                    httpHeaderValue1 = "$__file{${cfg.datasources.influxdb.tokenPath}}";
                  };
                }
                ++ cfg.datasources.extra;
            in
              datasources;
          };
        };
      };
    };
  };
}
