{ depot, lib, pkgs, ... }:

let
  inherit (depot.ops) secrets;
in {
  services.home-assistant = {
    enable = true;
    configWritable = true;
    lovelaceConfigWritable = true;
    config = {
      homeassistant = {
        name = "117 Malden Road";
        time_zone = "Europe/London";
        external_url = "https://ha.lukegb.com";
        internal_url = "https://ha.lukegb.com";
      };
      notify = [{
        name = "pushover-luke";
        platform = "pushover";
        api_key = secrets.pushover.tokens.home-assistant;
        user_key = secrets.pushover.userKey;
      }];
      http = {
        use_x_forwarded_for = true;
        trusted_proxies = [ "::1" "127.0.0.1" ];
      };
      lifx.light = {};
      light = [{
        platform = "group";
        name = "Bedroom Lights";
        entities = map (x: "light.overhead_${x.side}_${x.n}") (lib.cartesianProductOfSets { side = [ "left" "right" ]; n = [ "1" "2" "3" "4" ]; });
      }];
      cast = {};
      plex = {};
      default_config = {};

      google_assistant = {
        project_id = "malden-homeassistant";
        service_account = secrets.homeAssistant.googleServiceAccount;
        report_state = true;
        exposed_domains = [
          "light"
        ];
      };

      scene = let
        bedroomOverhead = x: ({
          color_mode = "color_temp";
          brightness = 255;
          color_temp = 316;
        } // x);
        bedroomRing = x: ({
          color_mode = "color_temp";
          brightness = 69;
          color_temp = 142;
        } // x);
        entityDefs = ring: overhead: let o = bedroomOverhead { state = overhead; }; in {
          "light.bedside_lamp" = bedroomRing { state = ring; };
          "light.overhead_left_1" = o;
          "light.overhead_left_2" = o;
          "light.overhead_left_3" = o;
          "light.overhead_left_4" = o;
          "light.overhead_right_1" = o;
          "light.overhead_right_2" = o;
          "light.overhead_right_3" = o;
          "light.overhead_right_4" = o;
        };
      in [{
        name = "Bedroom Reset";
        entities = entityDefs "off" "on";
      } {
        name = "Bedroom Off";
        entities = entityDefs "off" "off";
      }];

      automation = let
        base = extra: alias: webhook_id: ({
          inherit alias;
          trigger = [{
            inherit webhook_id;
            platform = "webhook";
          }];
        } // extra);
        toggleEntity = entity_id: base {
          action = [{
            service = "homeassistant.toggle";
            target.entity_id = entity_id;
          }];
        };
        playPauseEntity = entity_id: base {
          action = [{
            service = "media_player.media_play_pause";
            target.entity_id = entity_id;
          }];
        };
        scene = scene_entity_id: base {
          action = [{
            service = "scene.turn_on";
            target.entity_id = scene_entity_id;
            data.transition = 1.0;
          }];
        };
      in [
        (toggleEntity "light.bedroom_lights" "Desk LHS Switch press - toggle room" "flic_desk_lhs")
        (scene "scene.bedroom_reset" "Desk LHS Switch hold - reset room" "flic_desk_lhs_hold")

        (toggleEntity "light.bedside_lamp" "Desk RHS Switch press - toggle bedside" "flic_desk_rhs")

        (toggleEntity "light.bedroom_lights" "Bed LHS Switch press - toggle room" "flic_bed_lhs")
        (scene "scene.bedroom_reset" "Bed LHS Switch dblpress - reset room" "flic_bed_lhs_dbl")
        (scene "scene.bedroom_off" "Bed LHS Switch dblpress - reset room" "flic_bed_lhs_hold")

        (playPauseEntity "media_player.nebula" "Bed RHS Switch press - play/pause nebula" "flic_bed_rhs")

        (toggleEntity "light.bedroom_lights" "Bedroom Door Switch press - toggle room" "flic_bedroom_door")
        (scene "scene.bedroom_off" "Bedroom Door Switch dblpress - room off" "flic_bedroom_door_dbl")
        (scene "scene.bedroom_off" "Bedroom Door Switch hold - room off" "flic_bedroom_door_hold")
      ];
    };
    lovelaceConfig = {
      title = "117 Malden Road";
      views = [{
        title = "Bedroom Lights";
        cards = [{
          type = "light";
          entity = "light.bedroom_lights";
        } {
          type = "light";
          entity = "light.bedside_lamp";
          icon = "mdi:webcam";
        }];
      }];
    };
  };

  services.nginx.virtualHosts."ha.lukegb.com" = {
    forceSSL = true;
    locations."/" = {
      proxyPass = "http://localhost:8123/";
      proxyWebsockets = true;
    };
  };
  my.vault.acmeCertificates."ha.lukegb.com" = {
    hostnames = [ "ha.lukegb.com" ];
    nginxVirtualHosts = [ "ha.lukegb.com" ];
  };
}