import { Injectable } from '@nestjs/common';
import { UserMessageDto } from 'src/chatbot/dtos/user-message.dto';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
import { PageContextDto } from 'src/chatbot/dtos/page-context.dto';
import { Message } from 'src/chatbot/dtos/dialog-message.dto';

@Injectable()
export class ChatbotService {
    constructor(private readonly httpService: HttpService) { }
    sessionId: string = 'testing';

    async pingExternalApi(body: UserMessageDto, email: string): Promise<Message> {
        const { data } = await firstValueFrom(this.httpService.post(
            'https://n8nai.targo.ca/webhook/chatty-Mcbot', 
            { userInput: body.userInput, userId: email, sessionId: this.sessionId, pageContext: body.pageContext ?? undefined }
        ));

        return {
            text: data[0].output,
            sent: false,
        };
    }

    // async sendPageContext(body: PageContextDto, email: string) {
    //     const { data } = await firstValueFrom(
    //         this.httpService.post(
    //             'https://n8nai.targo.ca/webhook/chatty-Mcbot',
    //             { features: body, userId: email, userInput: '' },
    //         ),
    //     );
    //     return data;
    // }

    // Will have to modify later on to accomodate newer versions of User Auth/User type Structure
    // async sendUserContext(user_email: string) {
    //     if (!this.sessionId) {
    //         this.sessionId = 'SessionId = ' + user_email;
    //     }

    //     const response = await firstValueFrom(
    //         this.httpService.post(
    //             'https://n8nai.targo.ca/webhook/chatty-Mcbot',
    //             {
    //                 userId: this.sessionId,
    //                 userInput: '',
    //                 features: '',
    //             },
    //             { headers: { 'Content-Tyoe': 'application/json' } },
    //         ),
    //     );
    //     return response.data;
    // }
}
