import { Injectable } from '@nestjs/common';
import Groq from 'groq-sdk';
import OpenAI from 'openai';
import { BusinessListingCriteria } from '../models/main.model';

const businessListingCriteriaStructure = {
  criteriaType: 'business | commercialProperty | broker',
  types: "'Automotive'|'Industrial Services'|'Food and Restaurant'|'Real Estate'|'Retail'|'Oilfield SVE and MFG.'|'Service'|'Advertising'|'Agriculture'|'Franchise'|'Professional'|'Manufacturing'",
  city: 'string',
  state: 'string',
  county: 'string',
  minPrice: 'number',
  maxPrice: 'number',
  minRevenue: 'number',
  maxRevenue: 'number',
  minCashFlow: 'number',
  maxCashFlow: 'number',
  minNumberEmployees: 'number',
  maxNumberEmployees: 'number',
  establishedSince: 'number',
  establishedUntil: 'number',
  realEstateChecked: 'boolean',
  leasedLocation: 'boolean',
  franchiseResale: 'boolean',
  title: 'string',
  brokerName: 'string',
  searchType: "'exact' | 'radius'",
  radius: "'0' | '5' | '20' | '50' | '100' | '200' | '300' | '400' | '500'",
};
@Injectable()
export class AiService {
  private readonly openai: OpenAI;
  private readonly groq: Groq;
  constructor() {
    this.openai = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY, // Verwenden Sie Umgebungsvariablen für den API-Schlüssel
    });
    this.groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
  }

  async getBusinessCriteria(query: string): Promise<BusinessListingCriteria> {
    // const prompt = `
    //                 Dieses Objekt ist wie folgt definiert: ${JSON.stringify(businessListingCriteriaStructure)}.
    //                 Die Antwort darf nur das von dir befüllte JSON als unformatierten Text enthalten so das es von mir mit JSON.parse() einlesbar ist!!!!
    //                 Falls es Ortsangaben gibt, dann befülle City, County und State wenn möglich Die Suchanfrage des Users lautet: "${query}"`;
    const prompt = `The Search Query of the User is: "${query}"`;
    let response = null;
    try {
      // response = await this.openai.chat.completions.create({
      //   model: 'gpt-4o-mini',
      //   //model: 'gpt-3.5-turbo',
      //   max_tokens: 300,
      //   messages: [
      //     {
      //       role: 'system',
      //       content: `Please create unformatted JSON Object from a user input.
      //                       The type is: ${JSON.stringify(businessListingCriteriaStructure)}.,
      //         If location details available please fill city, county and state as State Code`,
      //     },
      //   ],
      //   temperature: 0.5,
      //   response_format: { type: 'json_object' },
      // });

      response = await this.groq.chat.completions.create({
        messages: [
          {
            role: 'system',
            content: `Please create unformatted JSON Object from a user input.
                      The type must be: ${JSON.stringify(businessListingCriteriaStructure)}.
                      If location details available please fill city, county and state as State Code`,
          },
          {
            role: 'user',
            content: prompt,
          },
        ],
        model: 'llama-3.1-70b-versatile',
        //model: 'llama-3.1-8b-instant',
        temperature: 0.2,
        max_tokens: 300,
        response_format: { type: 'json_object' },
      });

      const generatedCriteria = JSON.parse(response.choices[0]?.message?.content);
      return generatedCriteria;

      // return response.choices[0]?.message?.content;
    } catch (error) {
      console.error(`Error calling GPT-4 API: ${response.choices[0]}`, error);
      throw new Error('Failed to generate business criteria');
    }
  }
}
