const TdPrompt = require('@tdprompt/td-prompt.js');
const GeminiClient = require('@tdprompt/genkit-client.js');
const GenkitModels = require('@tdprompt/genkit-models.js');

// Define the API models to test and report
// .....

// TdPrompt will run and benchmark the scenarios
(new TdPrompt(apiModels)).benchmark(function (params) {

    describe(`Restaurant orders agent - evaluating ${params?.model}`, function () {
        let geminiClient, prompt, json;

        // Setup current model from the list
        before(async function () {
            // Load prompt template, we use EJS for flexible parametrization
            prompt = await readFile(path.join('.', 'src', 'prompt', `prompt-init-specs.md`), 'utf8');
            prompt = ejs.compile(prompt)

            // Instantiate Gemini on specific actual model
            geminiClient = new GeminiClient(params.apiKey);
            geminiClient.apiModel = params.model
            
            json = {}
        });

        describe('order creation', function () {

            it('must send the prompt message to the LLM API', async function () {
                const result = await geminiClient.send(prompt({
                    incomingMessage: `
                    I want a pepperoni pizza, add pepper on top, with a cocacola and a sprite with no ice, 
                    also a side of nuggets and a chicken tenders, 
                    2 ice creams, 3 chocolate ice cream add topping and sprinkles,
                    and 3 Caesar salads no salt
                    `,
                }), this);
                assert.ok(result?.text);
                
                // Sanitize the response text to be a valid JSON
                const response = jsonrepair(result.text);
                assert.ok(response);

                // Assert expectations
                json = JSON.parse(response);
                assert.ok(json?.data);
                assert.ok(json?.resume);
            });
            
            it('must create an order with 8 items', async function () {
                let expected = 8
                assert.equal(json?.data?.customer, undefined);
                assert.equal(json?.data?.phoneNumber, undefined);
                assert.equal(json?.data?.menuItems?.length, expected, `the order must have ${expected} menu items`);
            });
        });
    });
});