import axios from 'axios';
import dotenv from 'dotenv';

dotenv.config();

export const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:3000/api';
export const ASSET_BASE_URL = process.env.ASSET_BASE_URL || 'http://localhost:3000';

const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 8000
});

// Resolves an uploaded file's relative path (e.g. /uploads/tours/x.jpg) to a full URL.
// Leaves absolute URLs (http...) and template-bundled assets (/assets/...) untouched.
export const resolveUpload = (path) => {
  if (!path) return null;
  if (path.startsWith('http://') || path.startsWith('https://')) return path;
  if (path.startsWith('/assets')) return path;
  return `${ASSET_BASE_URL}${path}`;
};

export default api;
