{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.settings.containers.homepage;
in {
  options = {
    settings.containers.homepage.enable = lib.mkOption {
      type = lib.types.bool;
      default = false;
      description = ''
        Enable homepage container
      '';
    };
  };

  config = mkIf cfg.enable {
    services.podman.containers.homepage = {
      image = "ghcr.io/gethomepage/homepage:latest";
      network = "proxy";
      volumes = [
        "%h/containers/homepage/config:/app/config" 
        "%h/containers/homepage/config/settings.yaml:/app/config/settings.yaml"
        "%h/containers/homepage/config/services.yaml:/app/config/services.yaml"
        "%h/containers/homepage/config/docker.yaml:/app/config/docker.yaml"
        "/run/user/1000/podman/podman.sock:/var/run/podman.sock:ro"
      ];
      userNS = "keep-id";
      environment = {
        HOMEPAGE_ALLOWED_HOSTS = "home.tbmrs.nl";
      };
    };

    settings.containers.caddy.routes.tbmrs.routes.homepage = {
      host = "home";
      url = "homepage:3000"; 
    };

    home.file."containers/homepage/config/settings.yaml".source = (pkgs.formats.yaml { }).generate "settings" {
      title = "Timo's Server";
      description = "server from Timo";
      theme = "dark";
      color = "slate";
      layout = {
        Services = {
          style = "row";
          columns = "4";
        };
        Infra = {
          style = "row";
          columns = "4";
        };
      };
    };

    home.file."containers/homepage/config/services.yaml".source = (pkgs.formats.yaml { }).generate "services" [
      {
        "Services" = [
          {
            "Forgejo" = {
              href = "https://git.tbmrs.nl";
              description = "Git server";
              icon = "forgejo";
              server = "podman";
              container = "forgejo";
            };    
          }
          {
            "Immich" = {
              href = "https://photos.tbmrs.nl";
              description = "Photo's and videos";
              icon = "immich";
              server = "podman";
              container = "immich-server";
            };    
          }
          {
            "Pingvin" = {
              href = "https://share.tbmrs.nl";
              description = "File sharing";
              icon = "pingvin-share";
              server = "podman";
              container = "pingvin-share";
            };    
          }
          {
            "Vaultwarden" = {
              href = "https://vault.local.tbmrs.nl";
              description = "Password management";
              icon = "vaultwarden";
              server = "podman";
              container = "vaultwarden";
            };    
          }
          {
            "Paperless" = {
              href = "https://paperless.local.tbmrs.nl";
              description = "Documents management";
              icon = "paperless-ngx";
              server = "podman";
              container = "paperless-ngx";
            };    
          }
          {
            "Home Assistant" = {
              href = "https://home-assistant.local.tbmrs.nl";
              description = "Home automation";
              icon = "home-assistant";
              server = "podman";
              container = "homeassistant";
            };    
          }
          {
            "Syncthing" = {
              href = "https://syncthing.local.tbmrs.nl";
              description = "File syncronisation";
              icon = "syncthing";
              server = "podman";
              container = "syncthing";
            };    
          }
          {
            "Dufs" = {
              href = "https://files.tbmrs.nl";
              description = "File management";
              icon = "dufs";
              server = "podman";
              container = "dufs";
            };    
          }
          {
            "Linkding" = {
              href = "https://links.local.tbmrs.nl";
              description = "Bookmarks";
              icon = "linkding";
              server = "podman";
              container = "linkding";
            };    
          }
          {
            "Stalwart" = {
              href = "https://mail.tbmrs.nl";
              description = "Mailserver";
              icon = "stalwart";
              server = "podman";
              container = "stalwart";
            };    
          }
        ];
      }
      {
        "Infra" = [
          {
            "Kanidm" = {
              href = "https://auth.tbmrs.nl";
              description = "Oauth2 and ldap provider";
              icon = "kanidm";
              server = "podman";
              container = "kanidm";
            };    
          }
          {
            "Uptime Kuma" = {
              href = "https://uptime.tbmrs.nl";
              description = "Uptime and status";
              icon = "uptime-kuma";
              server = "podman";
              container = "uptime-kuma";
            };    
          }
          {
            "Beszel" = {
              href = "https://monitor.local.tbmrs.nl";
              description = "Server monitoring";
              icon = "beszel";
              server = "podman";
              container = "beszel";
            };    
          }
        ];
      }
    ];

    home.file."containers/homepage/config/docker.yaml".source = (pkgs.formats.yaml {}).generate "docker" {
      podman = {
        socket = "/var/run/podman.sock";  
      };
    };
  };
}
