// @ts-check
const path = require("path");
const fs = require("fs");

/**
 * @typedef IConfiguration
 * @property {string} site
 * @property {string} [sentry_dsn_js]
 * @property {boolean} development_mode
 * @property {boolean} automatic_migrations
 * @property {IServerConfig} webserver
 * @property {IArchiveServerConfig} [archive_server]
 */

/**
 * @typedef IServerConfig
 * @property {IUIConfig} ui
 * @property {number} port
 * @property {string} [base_url]
 */

/**
 * @typedef IUIConfig
 * @property {IHomeConfig} home
 * @property {{paysite_list: string[], artists_or_creators: string}} config
 * @property {IMatomoConfig} [matomo]
 * @property {ISidebarConfig} [sidebar]
 * @property {unknown[]} sidebar_items
 * @property {unknown[]} [footer_items]
 * @property {IBannerConfig} [banner]
 * @property {IAdsConfig} [ads]
 */

/**
 * @typedef IMatomoConfig
 * @property {boolean} enabled
 * @property {string} plain_code b64-encoded string
 * @property {string} tracking_domain
 * @property {string} tracking_code
 * @property {string} site_id
 */

/**
 * @typedef ISidebarConfig
 * @property {boolean} [disable_dms]
 * @property {boolean} [disable_faq]
 * @property {boolean} [disable_filehaus]
 */

/**
 * @typedef IBannerConfig
 * @property {string} [global] b64-encoded string
 * @property {string} [welcome] b64-encoded string
 */

/**
 * @typedef IHomeConfig
 * @property {string} [site_name]
 * @property {string} [mascot_path]
 * @property {string} [logo_path]
 * @property {string} [welcome_credits] b64-encoded string
 * @property {string} [home_background_image]
 * @property {{ title: string, date: string, content: string }[]} [announcements]
 */

/**
 * @typedef IAdsConfig
 * @property {string} [header] b64-encoded string
 * @property {string} [middle] b64-encoded string
 * @property {string} [footer] b64-encoded string
 * @property {string} [slider] b64-encoded string
 * @property {string} [video] b64-encoded JSON string
 */

/**
 * @typedef IArchiveServerConfig
 * @property {boolean} [enabled]
 */

const configuration = getConfiguration();
const apiServerBaseURL = configuration.webserver.base_url;
const sentryDSN = configuration.sentry_dsn_js;
const apiServerPort = !apiServerBaseURL
  ? undefined
  : configuration.webserver.port;
const siteName = configuration.webserver.ui.home.site_name || "Kemono";
const homeBackgroundImage =
  configuration.webserver.ui.home.home_background_image;
const homeMascotPath = configuration.webserver.ui.home.mascot_path;
const homeLogoPath = configuration.webserver.ui.home.logo_path;
const homeWelcomeCredits = configuration.webserver.ui.home.welcome_credits;
const homeAnnouncements = configuration.webserver.ui.home.announcements;
// TODO: in development it should point to webpack server
const kemonoSite = configuration.site || "http://localhost:5000";
const paysiteList = configuration.webserver.ui.config.paysite_list;
const artistsOrCreators =
  configuration.webserver.ui.config.artists_or_creators ?? "Artists";
const disableDMs = configuration.webserver.ui.sidebar?.disable_dms ?? true;
const disableFAQ = configuration.webserver.ui.sidebar?.disable_faq ?? true;
const disableFilehaus =
  configuration.webserver.ui.sidebar?.disable_filehaus ?? true;
const sidebarItems = configuration.webserver.ui.sidebar_items;
const footerItems = configuration.webserver.ui.footer_items;
const bannerGlobal = configuration.webserver.ui.banner?.global;
const bannerWelcome = configuration.webserver.ui.banner?.welcome;
const headerAd = configuration.webserver.ui.ads?.header;
const middleAd = configuration.webserver.ui.ads?.middle;
const footerAd = configuration.webserver.ui.ads?.footer;
const sliderAd = configuration.webserver.ui.ads?.slider;
const videoAd = configuration.webserver.ui.ads?.video;
const isArchiveServerEnabled = configuration.archive_server?.enabled ?? false;
const analyticsEnabled = configuration.webserver.ui.matomo?.enabled ?? false;
const analyticsCode = configuration.webserver.ui.matomo?.plain_code;
const iconsPrepend = process.env.ICONS_PREPEND || "";
const bannersPrepend = process.env.BANNERS_PREPEND || "";
const thumbnailsPrepend = process.env.THUMBNAILS_PREPEND || "";
const creatorsLocation = process.env.CREATORS_LOCATION || "";

/**
 * @TODO config validation
 * @returns {IConfiguration}
 */
function getConfiguration() {
  const configPath = path.resolve(__dirname, "..", "..", "config.json");
  // TODO: async reading
  const fileContent = fs.readFileSync(configPath, { encoding: "utf8" });
  /**
   * @type {IConfiguration}
   */
  const config = JSON.parse(fileContent);

  return config;
}

module.exports = {
  kemonoSite,
  sentryDSN,
  siteName,
  iconsPrepend,
  bannersPrepend,
  thumbnailsPrepend,
  creatorsLocation,
  artistsOrCreators,
  disableDMs,
  disableFAQ,
  disableFilehaus,
  sidebarItems,
  footerItems,
  bannerGlobal,
  bannerWelcome,
  homeBackgroundImage,
  homeMascotPath,
  homeLogoPath,
  paysiteList,
  homeWelcomeCredits,
  homeAnnouncements,
  headerAd,
  middleAd,
  footerAd,
  sliderAd,
  videoAd,
  isArchiveServerEnabled,
  apiServerBaseURL,
  apiServerPort,
  analyticsEnabled,
  analyticsCode,
};
