{ config, lib, ... }:
let
  inherit (config) pages;
  cfg = config;
in
{
  imports = lib.nixFiles ./.;

  collections.news.type = cfg.content-types.article;
  collections.events.type = cfg.content-types.event;

  pages.index =
    { config, link, ... }:
    {
      title = "Welcome to the Fediversity project";
      description = "Fediversity web site";
      summary = ''
        This web site hosts up-to-date information about the the NGI Zero Fediversity project.
      '';
      body = ''
        ${pages.fediversity.summary}

        [Learn more about Fediversity](${link pages.fediversity})
      '';
      outputs.html = (cfg.templates.html.page config).override (
        _final: prev: {
          html = {
            head.title.text = "Fediversity";
            head.link.stylesheets = prev.html.head.link.stylesheets ++ [
              { href = "${link cfg.assets."index.css"}"; }
            ];
            body.content =
              let
                to-section =
                  {
                    heading,
                    body,
                    attrs ? { },
                  }:
                  {
                    section = {
                      heading.content = heading;
                      inherit attrs;
                      content = [
                        (cfg.templates.html.markdown {
                          name = "${config.name}-${lib.slug heading}";
                          inherit body;
                        })
                      ];
                    };
                  };
              in
              [
                (lib.head prev.html.body.content)
                {
                  section = {
                    attrs = { };
                    heading.content = config.title;
                    content =
                      [
                        (cfg.templates.html.markdown { inherit (config) name body; })
                      ]
                      ++ (map to-section [
                        {
                          heading = "Fediversity grants";
                          body = ''
                            ${pages.grants.summary}

                            [Learn more about Fediversity grants](${link pages.grants})
                          '';
                        }
                        {
                          heading = "Consortium";
                          body = ''
                            The Consortium behind the Fediversity project is a cooperation between NLnet, Open Internet Discourse Foundation, NORDUnet and Tweag.

                            ${toString (
                              map
                                (partner: ''
                                  ### ${partner.title}

                                  ${partner.summary}

                                  [Read more about ${partner.title}](${link partner})
                                '')
                                (
                                  with pages;
                                  [
                                    nlnet
                                    oid
                                    tweag
                                    nordunet
                                  ]
                                )
                            )}
                          '';
                        }
                        {
                          heading = "Fediverse explained";
                          body = ''
                            ${toString (
                              map
                                (role: ''
                                  ### ${role.title}

                                  ${role.summary}

                                  [Read more about ${role.title}](${link role})
                                '')
                                (
                                  with pages;
                                  [
                                    individuals
                                    developers
                                    european-commission
                                  ]
                                )
                            )}
                          '';
                        }
                      ]);
                  };
                }
              ]
              ++ (map to-section [
                {
                  heading = "News";
                  attrs = {
                    class = [ "collection" ];
                  };
                  body =
                    let
                      sorted = with lib; reverseList (sortOn (entry: entry.date) cfg.collections.news.entry);
                    in
                    lib.join "\n" (
                      map (article: ''
                        - ${article.date} [${article.title}](${link article})
                      '') sorted
                    );
                }
                {
                  heading = "Events";
                  attrs = {
                    class = [ "collection" ];
                  };
                  body =
                    let
                      sorted = with lib; reverseList (sortOn (entry: entry.start-date) cfg.collections.events.entry);
                    in
                    lib.join "\n" (
                      map (article: ''
                        - ${article.start-date} [${article.title}](${link article})
                      '') sorted
                    );
                }
              ]);
          };

        }
      );
    };

  assets."index.css".path =
    with lib;
    builtins.toFile "index.css" ''
      section h1, section h2, section h3
      {
        text-align: center;
      }
      section h1 {
        font-size: 3em;
      }
      section h2 {
        font-size: 2.5em;
      }
      section h3 {
        font-size: 1.5em;
      }
      section.collection h1 {
        font-size: 2em;
        text-align: left;
      }
    '';
}