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

dotenv.config();

const fuelixClient = axios.create({
  baseURL: process.env.FUELIX_API_URL,
  headers: {
    'Authorization': `Bearer ${process.env.FUELIX_API_KEY}`,
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
});

export async function generateFuelixCompletion(messages: { role: string; content: string }[], model: string = 'gpt-4o-mini-2024-07-18'): Promise<string> {
  const response = await fuelixClient.post('/chat/completions', {
    model,
    messages,
  });
  return response.data.choices[0].message.content;
}

export async function generateFuelixPromptCompletion(prompt: string): Promise<string> {
  const response = await fuelixClient.post('/completions', {
    model: process.env.FUELIX_MODEL_NAME,
    prompt,
  });
  return response.data.choices[0].message.content;
}

export class FuelixClient {
  private client;

  constructor() {
    this.client = axios.create({
      baseURL: process.env.FUELIX_API_URL,
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${process.env.FUELIX_API_KEY}`
      }
    });
  }

  async analyzeChanges(diff: string): Promise<string> {
    try {
      const response = await this.client.post('/completions', {
        model: process.env.FUELIX_MODEL_NAME,
        prompt: `Analyze the following code changes and generate technical documentation:\n\n${diff}`,
        max_tokens: 500
      });

      return response.data.choices[0].text;
    } catch (error) {
      console.error('Error analyzing changes:', error);
      throw error;
    }
  }

  async generateTechnicalDetails(analysis: string): Promise<string> {
    try {
      const response = await this.client.post('/completions', {
        model: process.env.FUELIX_MODEL_NAME,
        prompt: `Based on the following analysis, provide detailed technical information about the implementation:\n\n${analysis}`,
        max_tokens: 300
      });

      return response.data.choices[0].text;
    } catch (error) {
      console.error('Error generating technical details:', error);
      throw error;
    }
  }

  async identifyRelatedComponents(analysis: string): Promise<string> {
    try {
      const response = await this.client.post('/completions', {
        model: process.env.FUELIX_MODEL_NAME,
        prompt: `Identify and list the related components affected by these changes:\n\n${analysis}`,
        max_tokens: 200
      });

      return response.data.choices[0].text;
    } catch (error) {
      console.error('Error identifying related components:', error);
      throw error;
    }
  }

  async generateAdditionalNotes(analysis: string): Promise<string> {
    try {
      const response = await this.client.post('/completions', {
        model: process.env.FUELIX_MODEL_NAME,
        prompt: `Generate any additional notes or considerations based on these changes:\n\n${analysis}`,
        max_tokens: 200
      });

      return response.data.choices[0].text;
    } catch (error) {
      console.error('Error generating additional notes:', error);
      throw error;
    }
  }
} 