/** @format */

import { Platform } from "react-native";

const DEV_URL = Platform.select({
  ios: "http://localhost:8000",
  android: "http://10.0.2.2:8000",
  default: "http://localhost:8000",
});

const DEFAULT_SERVER_URL = "https://investra-backend.softivus.com";
const DEFAULT_API_VERSION_PATH = "/api/v1";
const DEFAULT_TOKEN_NAME = "token";
const DEFAULT_MOBILE_APP_KEY = "1234567890";

const trimTrailingSlash = (value: string) => value.replace(/\/+$/, "");
const ensureLeadingSlash = (value: string) =>
  value.startsWith("/") ? value : `/${value}`;

const getAppEnv = () => process.env.EXPO_PUBLIC_APP_ENV?.trim() || "production";

const getServerUrl = () => {
  if (getAppEnv() === "development") {
    return DEV_URL;
  }

  const configuredUrl = process.env.EXPO_PUBLIC_API_BASE_URL?.trim();
  return configuredUrl ? trimTrailingSlash(configuredUrl) : DEFAULT_SERVER_URL;
};

const getApiVersionPath = () => {
  const configuredPath =
    process.env.EXPO_PUBLIC_API_VERSION_PATH?.trim() || DEFAULT_API_VERSION_PATH;

  return ensureLeadingSlash(configuredPath);
};

export const APP_ENV = getAppEnv();
export const SERVER_URL = getServerUrl();
export const API_VERSION_PATH = getApiVersionPath();
export const API_BASE_URL = `${SERVER_URL}${API_VERSION_PATH}`;
export const TOKEN_NAME = process.env.EXPO_PUBLIC_TOKEN_NAME || DEFAULT_TOKEN_NAME;
export const MOBILE_APP_KEY = Number(
  process.env.EXPO_PUBLIC_MOBILE_APP_KEY || DEFAULT_MOBILE_APP_KEY,
);

export const ONBOARDING_POSITION = "ONBOARDING_POSITION";

export const MAINTENANCE = "MAINTENANCE";

export const ONBOARDING_STEPS = {
  verification: {
    value: "verification",
    url: "/verify-otp",
  },
  kyc: {
    value: "kyc",
    url: "/kyc",
  },
  waiting: {
    value: "waiting",
    url: "/waiting",
  },
  completed: {
    value: "completed",
    url: "/home",
  },
};

/**
 * check if the user is trying to access protected paths without a valid token also check if the user is verifying their email or phone
 * if the user is not authenticated, redirect to the sign-in page
 * if the user is authenticated and trying to access the verification or kyc page, redirect
 */
export const PROTECTED_PATHS = [
  "/payment",
  "/wallet/deposit",
  "/wallet/withdraw",
  "/wallet/transactions",
  "/my-shares",
  "/my-shares-details",
  "/for-sale",
  "/wishlist",
  "/roi-recieved",
  "/upcoming-payouts",
  "/edit-profile",
  "/edit-security",
  "/change-password",
  "/security-settings",
  "/account-activity",
  "/contact-information",
  "/notifications",
  "/support",
  "/kyc",
  "/verification-status",
];
