const { getSetting } = require("./settings");

const PLATFORM_DEFS = [
  {
    id: "discord",
    label: "Discord",
    enabledKey: "platform_discord_enabled",
    enabledByDefault: true,
    supported: true,
    supportsLogin: true,
    supportsLink: true,
    wizardPath: "/admin/discord-wizard",
    loginPath: "/auth/discord",
    linkPath: "/auth/discord",
    badge: "discord",
    isConfigured: () =>
      Boolean(
        getSetting("discord_client_id") &&
          getSetting("discord_client_secret") &&
          getSetting("discord_guild_id") &&
          getSetting("discord_bot_token")
      )
  },
  {
    id: "kick",
    label: "Kick",
    enabledKey: "platform_kick_enabled",
    enabledByDefault: false,
    supported: false,
    supportsLogin: false,
    supportsLink: false,
    wizardPath: null,
    loginPath: null,
    linkPath: null,
    badge: "kick",
    isConfigured: () => false
  },
  {
    id: "twitch",
    label: "Twitch",
    enabledKey: "platform_twitch_enabled",
    enabledByDefault: true,
    supported: true,
    supportsLogin: true,
    supportsLink: true,
    wizardPath: "/admin/twitch-wizard",
    loginPath: "/auth/twitch/login",
    linkPath: "/auth/twitch",
    badge: "twitch",
    isConfigured: () =>
      Boolean(getSetting("twitch_client_id") && getSetting("twitch_client_secret"))
  },
  {
    id: "youtube",
    label: "YouTube",
    enabledKey: "platform_youtube_enabled",
    enabledByDefault: false,
    supported: true,
    supportsLogin: true,
    supportsLink: true,
    wizardPath: "/admin/youtube-wizard",
    loginPath: "/auth/youtube/login",
    linkPath: "/auth/youtube",
    badge: "youtube",
    isConfigured: () =>
      Boolean(getSetting("youtube_client_id") && getSetting("youtube_client_secret"))
  }
];

function getPlatforms() {
  return PLATFORM_DEFS.slice().sort((a, b) => a.label.localeCompare(b.label));
}

function getPlatformById(id) {
  return PLATFORM_DEFS.find((platform) => platform.id === id) || null;
}

function isPlatformEnabled(id) {
  const platform = getPlatformById(id);
  if (!platform) {
    return false;
  }
  return Boolean(getSetting(platform.enabledKey, platform.enabledByDefault));
}

function isPlatformConfigured(id) {
  const platform = getPlatformById(id);
  if (!platform || !platform.supported) {
    return false;
  }
  if (typeof platform.isConfigured !== "function") {
    return false;
  }
  return Boolean(platform.isConfigured());
}

function getPlatformStatus() {
  return getPlatforms().map((platform) => ({
    ...platform,
    enabled: isPlatformEnabled(platform.id),
    configured: isPlatformConfigured(platform.id)
  }));
}

function getEnabledPlatforms({ supportedOnly = true } = {}) {
  return getPlatforms().filter((platform) => {
    if (supportedOnly && !platform.supported) {
      return false;
    }
    return isPlatformEnabled(platform.id);
  });
}

function getEnabledPlatformIds({ supportedOnly = true } = {}) {
  return getEnabledPlatforms({ supportedOnly }).map((platform) => platform.id);
}

function getLoginPlatforms() {
  return getEnabledPlatforms({ supportedOnly: true }).filter(
    (platform) => platform.supportsLogin
  );
}

function getLinkPlatforms() {
  return getEnabledPlatforms({ supportedOnly: true }).filter(
    (platform) => platform.supportsLink
  );
}

function getPlatformLabel(id) {
  const platform = getPlatformById(id);
  return platform?.label || id;
}

function getPlatformBadge(id) {
  const platform = getPlatformById(id);
  return platform?.badge || id;
}

function normalizePlatformSelection(value, availablePlatforms) {
  const available =
    Array.isArray(availablePlatforms) && availablePlatforms.length
      ? availablePlatforms
      : getEnabledPlatformIds();
  if (!value) {
    return available;
  }
  const raw = value.toString().trim().toLowerCase();
  if (!raw) {
    return available;
  }
  if (raw === "all") {
    return available;
  }
  if (raw === "both") {
    return ["discord", "twitch"].filter((platform) => available.includes(platform));
  }
  const selected = raw
    .split(/[,\s]+/)
    .map((entry) => entry.trim())
    .filter(Boolean);
  const unique = Array.from(new Set(selected));
  return unique.filter((platform) => available.includes(platform));
}

function serializePlatformSelection(platforms) {
  if (!Array.isArray(platforms) || platforms.length === 0) {
    return "";
  }
  return Array.from(new Set(platforms)).join(",");
}

module.exports = {
  getPlatforms,
  getPlatformById,
  getPlatformStatus,
  isPlatformEnabled,
  isPlatformConfigured,
  getEnabledPlatforms,
  getEnabledPlatformIds,
  getLoginPlatforms,
  getLinkPlatforms,
  getPlatformLabel,
  getPlatformBadge,
  normalizePlatformSelection,
  serializePlatformSelection
};
