import axiosInstance from '@/services/axiosInstance'; // Try using alias path
import { MqttLogQueryRequest, MqttCommunicationLog, PageResponse } from '@/types/log.types';

// Define a generic BaseResponse type to match the backend structure
export interface BaseResponse<T> {
  code: number;
  data: T;
  message: string;
}

/**
 * Fetch MQTT communication logs from the backend.
 * @param params - The query parameters for fetching logs.
 * @returns A promise that resolves to a BaseResponse containing a page of MQTT communication logs.
 */
export const fetchMqttLogs = async (
  params: MqttLogQueryRequest
): Promise<BaseResponse<PageResponse<MqttCommunicationLog>>> => {
  try {
    // Clean up dateRange before sending to backend
    const backendParams = { ...params };
    if (backendParams.dateRange) {
      delete backendParams.dateRange;
    }

    const response = await axiosInstance.post<BaseResponse<PageResponse<MqttCommunicationLog>>>(
      '/api/admin/mqtt-log/list/page',
      backendParams
    );
    return response.data; // Axios wraps the actual server response in its own 'data' property
                          // So, response.data here IS the BaseResponse from the backend.
  } catch (error) {
    console.error('Error fetching MQTT logs:', error);
    // Consider throwing a more specific error or handling it as per your app's error strategy
    // For now, rethrow to be caught by the caller
    throw error;
  }
};

// You might need to create axiosInstance.ts if it doesn't exist
// Example for axiosInstance.ts:
// import axios from 'axios';
//
// const axiosInstance = axios.create({
//   baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '', // Your API base URL
//   headers: {
//     'Content-Type': 'application/json',
//   },
// });
//
// // Optional: Add interceptors for request/response, e.g., for auth tokens
// axiosInstance.interceptors.request.use(
//   (config) => {
//     // const token = localStorage.getItem('token'); // Or however you store your token
//     // if (token) {
//     //   config.headers.Authorization = `Bearer ${token}`;
//     // }
//     return config;
//   },
//   (error) => {
//     return Promise.reject(error);
//   }
// );
//
// export default axiosInstance; 