# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0

{ system ? builtins.currentSystem, ... }:
let
  depot = (import ./default.nix { inherit system; });
  pkgs = depot.third_party.nixpkgs;
  lib = pkgs.lib;
  prefixAttrs = prefix: lib.mapAttrsToList (name: value: { name = "${prefix}-${name}"; path = value; });

  ciBits = rec {
    x86_64-linux = {
      machine = depot.ops.nixos.systems;
      pkgs = builtins.removeAttrs depot.nix.pkgs [ "grafana-plugins" "windows" "unifiHacked" "nixpkgs-mozilla" "nightlyRust" "nightlyRustPlatform" "baserow" "baserow-oss" "authentik" "factorio-mods" "home-assistant-chip" ];
      pkg-baserow = {
        inherit (depot.nix.pkgs.baserow) backend web-frontend;
      };
      pkg-baserow-oss = {
        inherit (depot.nix.pkgs.baserow-oss) backend web-frontend;
      };
      pkg-authentik = {
        inherit (depot.nix.pkgs.authentik) docs webui pythonapp goserver server;
      };
      pkg-grafana-plugins = depot.nix.pkgs.grafana-plugins;
      web = builtins.removeAttrs (lib.filterAttrs (n: v: !lib.isFunction v) depot.web) [ "barf" ];
      web-barf = {
        inherit (depot.web.barf) frontend;
        inherit (depot.web.barf.sapi) sapid-wrapper;
      };
      trains = depot.go.trains.cmd.bins;
      other = {
        twitterchiver-archiver = depot.go.twitterchiver.archiver;
        twitterchiver-archiver-docker = depot.go.twitterchiver.archiver.dockerImage;
        twitternuke = depot.go.twitternuke;
        systemPathJSON = depot.ops.nixos.systemPathJSON;
        inherit (depot.go.nix) bcachegc bcacheup;
      };
      factorio = depot.ops.factorio;
      home-manager = depot.ops.home-manager-ext.built;
      rust = depot.rust;
    };
    aarch64-linux = builtins.removeAttrs x86_64-linux [ "home-manager" "pkg-authentik" "web-barf" ] // {
      pkgs = builtins.removeAttrs x86_64-linux.pkgs [ "lutris" "plex-pass" "sheepshaver" "fr24feed" "javaws-env" "world-of-goo2" ];
    };
    x86_64-darwin = {
      machine = depot.ops.nix-darwin.systems;
      home-manager = {
        inherit (depot.ops.home-manager-ext.built) base client;
      };
    };
    aarch64-darwin = x86_64-darwin;
  }.${system};

  entriesFilter = entries: assert builtins.isList entries; let
    notADerivationOrPath = builtins.filter (elem: !lib.isString elem.path && !lib.isDerivation elem.path) entries;
    notADerivationOrPath' = map (elem: elem.name) notADerivationOrPath;
  in assert lib.assertMsg ((lib.length notADerivationOrPath') == 0) "not a derivation or path: ${toString notADerivationOrPath'}"; entries;
  
  entries = entriesFilter (builtins.concatLists (lib.mapAttrsToList prefixAttrs ciBits));
  linkFarm = pkgs.linkFarm "ci" entries;

  targetNamer' = prefix: lib.mapAttrsToList (name: value: let
      prefixedName = prefix ++ [name];
      finalName = lib.concatStringsSep "." prefixedName;
    in
      if lib.isDerivation value then finalName
      else if lib.isAttrs value then targetNamer' prefixedName value
      else finalName
  );
  targetNamer = attrs: lib.flatten (targetNamer' [] attrs);

  delimiter = "HONKHONKHONKxxxHONKHONKHONK";
  forgejoMatrix = pkgs.writeText "forgejo-ci-matrix" ''
    machines<<${delimiter}
    ${builtins.toJSON (builtins.attrNames depot.ops.nixos.systems)}
    ${delimiter}
    targets<<${delimiter}
    ${builtins.toJSON (lib.filter (name: !lib.hasPrefix "machine." name) (targetNamer ciBits))}
    ${delimiter}
  '';
in
ciBits // {
  inherit forgejoMatrix linkFarm;
}