/**
 * ==================================================
 * Smart Attendance System
 * AI Client
 * ==================================================
 * Shared Axios client for Python AI service.
 * ==================================================
 */

const axios = require("axios");

const aiConfig = require("../../config/ai.config");


const aiClient = axios.create({

    baseURL: aiConfig.baseURL,

    timeout: aiConfig.timeout,

    headers: {

        Accept: "application/json"

    }

});


// ==========================================
// Request Logger
// ==========================================

aiClient.interceptors.request.use(

    (config) => {

        console.log(
            `[AI REQUEST] ${config.method?.toUpperCase()} ${config.baseURL}${config.url}`
        );

        return config;

    },

    (error) => Promise.reject(error)

);


// ==========================================
// Response Logger
// ==========================================

aiClient.interceptors.response.use(

    (response) => {

        console.log(

            `[AI RESPONSE] ${response.status} ${response.config.url}`

        );

        return response;

    },

    (error) => {

        if (error.response) {

            console.error(

                "[AI ERROR]",

                error.response.status,

                error.response.data

            );

        }

        else if (error.request) {

            console.error(

                "[AI ERROR] Python AI service is unreachable."

            );

        }

        else {

            console.error(

                "[AI ERROR]",

                error.message

            );

        }

        return Promise.reject(error);

    }

);


module.exports = aiClient;