module doc_reform.meta.metadoc;
template spineAbstraction() {
  import
    std.datetime;
  import
    doc_reform.meta,
    doc_reform.meta.metadoc_from_src,
    doc_reform.meta.conf_make_meta_structs,
    doc_reform.meta.conf_make_meta_json,
    doc_reform.meta.defaults,
    doc_reform.meta.rgx,
    doc_reform.io_in.paths_source,
    doc_reform.io_in.read_config_files,
    doc_reform.io_in.read_source_files,
    doc_reform.io_out.hub;
  mixin spineRgxIn;
  mixin spineBiblio;
  mixin spineRgxDocStructFlags;
  mixin outputHub;
  enum headBody { header, body_content, insert_file_list, image_list }
  enum makeMeta { make, meta }
  enum docAbst  { doc_abstract_obj, doc_has }
  static auto rgx = RgxI();
  @system auto spineAbstraction(E,P,O,M,S)(
    E _env,
    P program_info,
    O _opt_action,
    M _manifest,
    S _make_and_meta_struct
  ){
    { /+ document config/make file +/
      auto _config_document_struct = readConfigDoc!()(_manifest, _env);
      import doc_reform.meta.conf_make_meta_yaml;
      _make_and_meta_struct = _config_document_struct.configParseYAMLreturnSpineStruct!()(_make_and_meta_struct, _manifest, _opt_action);
    }
    /+ ↓ read file (filename with path) +/
    /+ ↓ file tuple of header and content +/
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("step1 commence → (get document header & body & insert file list & if needed image list)"
      );
    }
    auto _header_body_insertfilelist_imagelist
      = spineRawMarkupContent!()(_opt_action, _manifest.src.path_and_fn);
    static assert(_header_body_insertfilelist_imagelist.length==4);
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("- step1 complete");
    }
    debug(header_and_body) {
      writeln(header);
      writeln(_header_body_insertfilelist_imagelist.length);
      writeln(_header_body_insertfilelist_imagelist.length[headBody.body_content][0]);
    }
    /+ ↓ split header into make and meta +/
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("step2 commence → (read document header (yaml) return struct)");
    }
    import doc_reform.meta.conf_make_meta_yaml;
    _make_and_meta_struct =
      docHeaderMakeAndMetaTupYamlExtractAndConvertToStruct!()(
        _header_body_insertfilelist_imagelist[headBody.header],
        _make_and_meta_struct,
        _manifest,
        _opt_action,
      );
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("- step2 complete");
    }
    /+ ↓ document abstraction: process document, return abstraction as tuple +/
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("step3 commence → (document abstraction (da); da keys; segnames; doc_matters)");
    }
    auto da = docAbstraction!()(
      _header_body_insertfilelist_imagelist[headBody.body_content],
      _make_and_meta_struct,
      _opt_action,
      _manifest,
      true,
    );
    static assert(da.length==2);
    auto doc_abstraction = da[docAbst.doc_abstract_obj]; /+ head ~ toc ~ body ~ endnotes_seg ~ glossary ~ bibliography ~ bookindex ~ blurb; +/
    auto _doc_has_struct = da[docAbst.doc_has];
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("- step3 complete");
    }
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("step4 commence → (doc_matters)");
    }
    struct DocumentMatters {
      @safe auto generator_program() {
        struct Prog_ {
          @safe string project_name() {
            return "spine";
          }
          @safe string name() {
            return program_info.name;
          }
          @safe string ver() {
            return program_info.ver;
          }
          @trusted string name_and_version() {
            return format("%s-%s",
              name,
              ver,
            );
          }
          @safe string url_home() {
            return "http://sisudoc.org";
          }
          @safe string url_git() {
            return "https://git.sisudoc.org/software/sisu";
          }
          @safe auto compiler() {
            return program_info.compiler;
          }
          @safe auto stime() {
            return Clock.currTime(UTC()).toSimpleString();
          }
        }
        return Prog_();
      }
      @safe auto generated_time() {
        auto _st = Clock.currTime(UTC());
        auto _time = _st.year.to!string
          ~ "-" ~ _st.month.to!int.to!string // prefer as month number
          ~ "-" ~ _st.day.to!string
          ~ " [" ~ _st.isoWeek.to!string ~ "/" ~ _st.dayOfWeek.to!int.to!string ~ "]"
          ~ " " ~ _st.hour.to!string
          ~ ":" ~ _st.minute.to!string
          ~ ":" ~ _st.second.to!string;
        return _time;
      }
      @safe auto conf_make_meta() {
        return _make_and_meta_struct;
      }
      @safe auto has() {
        return _doc_has_struct;
      }
      @safe auto env() {
        struct Env_ {
          @safe auto pwd() {
            return _manifest.env.pwd;
          }
          @safe auto home() {
            return _manifest.env.home;
          }
        }
        return Env_();
      }
      @safe auto opt() {
        struct Opt_ {
          @safe auto action() {
            /+ getopt options, commandline instructions, raw
             - processing instructions --epub --html etc.
             - command line config instructions --output
            +/
            return _opt_action;
          }
        }
        return Opt_();
      }
      @safe auto src() {
        return _manifest.src;
      }
      @safe auto src_path_info() {
        return spinePathsSRC!()(_manifest.env.pwd, _manifest.src.file_with_absolute_path); // would like (to have and use) relative path
      }
      @safe auto pod() {
        return _manifest.pod;
      }
      @safe auto sqlite() {
        struct SQLite_ {
          @safe string filename() {
            string _fn = "";
            if (_opt_action.sqlite_filename.length > 0) {
              _fn = _opt_action.sqlite_filename;
            } else if (_make_and_meta_struct.conf.w_srv_db_sqlite.length > 0) {
              _fn = _make_and_meta_struct.conf.w_srv_db_sqlite;
            }
            return _fn;
          }
          @safe string cgi_filename() {
            string _fn = "";
            if (_opt_action.cgi_sqlite_search_filename.length > 0) {
              _fn = _opt_action.cgi_sqlite_search_filename;
            } else if (_make_and_meta_struct.conf.w_srv_cgi_search_script.length > 0) {
              _fn = _make_and_meta_struct.conf.w_srv_cgi_search_script;
            }
            return _fn;
          }
          @safe string cgi_filename_d() {
            string _fn = "";
            if (_opt_action.cgi_sqlite_search_filename_d.length > 0) {
              _fn = _opt_action.cgi_sqlite_search_filename_d;
            } else if (_make_and_meta_struct.conf.w_srv_cgi_search_script_raw_fn_d.length > 0) {
              _fn = _make_and_meta_struct.conf.w_srv_cgi_search_script_raw_fn_d;
            }
            return _fn;
          }
        }
        return SQLite_();
      }
      @safe auto output_path() {
        return _make_and_meta_struct.conf.output_path;
      }
      @safe auto srcs() {
        struct SRC_ {
          auto file_insert_list() {
            return _header_body_insertfilelist_imagelist[headBody.insert_file_list];
          }
          auto image_list() {
            return _doc_has_struct.imagelist;
          }
        }
        return SRC_();
      }
    }
    auto doc_matters = DocumentMatters();
    if ((_opt_action.debug_do)
    || (_opt_action.very_verbose)
    ) {
      writeln("- step4 complete");
    }
    auto t = tuple(doc_abstraction, doc_matters);
    return t;
  }
}