const formatServiceUrl = (url: string) => {
  if (!url) return "";
  return url.startsWith("http") ? url : `http://${url}`;
};

const baseUrlAuth = formatServiceUrl(
  process.env.NEXT_PUBLIC_AUTH_SERVICE_URL || "auth_service:8001"
);
const baseUrlRestriction = formatServiceUrl(
  process.env.NEXT_PUBLIC_RESTRICTION_SERVICE_URL || "restriction_service:8002"
);
const baseUrlApplication = formatServiceUrl(
  process.env.NEXT_PUBLIC_MANAGEMENT_SERVICE_URL || "management_service:8003"
);
const baseUrlAccount = formatServiceUrl(
  process.env.NEXT_PUBLIC_ACCOUNT_SERVICE_URL || "account_service:8004"
);
const baseUrlBuilding = formatServiceUrl(
  process.env.NEXT_PUBLIC_BUILDING_SERVICE_URL || "building_service:8006"
);
const baseUrlPeople = formatServiceUrl(
  process.env.NEXT_PUBLIC_VALIDATION_SERVICE_URL || "identity_service:8009"
);

// Types for better type safety
type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";

interface ApiResponse<T = any> {
  status: number;
  data: T;
  error?: string;
}

interface FetchOptions {
  method?: HttpMethod;
  cache?: boolean;
  timeout?: number;
}

const tokenSecret = process.env.TOKENSECRET_90 || "";

const cookieObject: any = {
  httpOnly: true,
  path: "/",
  sameSite: "none",
  secure: true,
  maxAge: 3600,
  priority: "high",
};

// Constants
const DEFAULT_TIMEOUT = 10000; // 10 seconds
const defaultHeaders = {
  accept: "application/json",
  language: "tr",
  domain: "management.com.tr",
  tz: "GMT+3",
  "Content-type": "application/json",
};
const DEFAULT_RESPONSE: ApiResponse = {
  error: "Hata tipi belirtilmedi",
  status: 500,
  data: {},
};

export type { HttpMethod, ApiResponse, FetchOptions };
export {
  DEFAULT_TIMEOUT,
  DEFAULT_RESPONSE,
  defaultHeaders,
  baseUrlAuth,
  baseUrlPeople,
  baseUrlApplication,
  baseUrlAccount,
  baseUrlBuilding,
  baseUrlRestriction,
  tokenSecret,
  cookieObject,
};
