import {Injectable} from '@angular/core';
import {ChatbotMessage} from './chatbot.message.type';


export class ChatbotSource {

  public title = "";
  public url = "";
  public description = "";

  constructor(config: any = {}) {
    this.title = config.title;
    this.url = config.url;
    this.description = config.description;
  }
}

export class ChatbotConversation {

  public messages: Array<ChatbotMessage> = []

  public name: string = "";
  public modelName: string = "Claude Girofle";
  public dateSeparator: string = "";
  public visibleInput: boolean = false;
  public pinned: boolean = false;
  public description: string = "Some 15 billion years ago the universe emerged from a hot, dense sea of...";
  public lastMessageDate: any = new Date().toLocaleDateString();

  public sources: Array<any> = [];
  public filters: Array<any> = [];

  // Static method to create a ChatbotConversation instance from a plain object
  static fromObject(obj: any): ChatbotConversation {
    if (!obj) return new ChatbotConversation();

    const conversation = new ChatbotConversation();
    conversation.name = obj.name || "";
    conversation.modelName = obj.modelName || "Claude Girofle";
    conversation.dateSeparator = obj.dateSeparator || "";

    let source1 = new ChatbotSource();
    source1.title = "abc-1258.pdf";
    source1.url = "https://example.com/source1";
    source1.description = "Description of Source 1";

    conversation.sources = obj.sources || [source1];
    conversation.filters = obj.filters || [];

    conversation.pinned = true;
    conversation.lastMessageDate = new Date().toLocaleDateString();

    // Convert messages to ChatbotMessage instances if they exist
    if (obj.messages && Array.isArray(obj.messages)) {
      conversation.messages = obj.messages.map((msg: any) => {
        if (msg instanceof ChatbotMessage) {
          return msg;
        } else {
          return new ChatbotMessage({
            kind: msg.kind || 'user',
            user: msg.user || {},
            content: msg.content || '',
            name: msg.name || '',
          });
        }
      });
    }

    return conversation;
  }

  addMessage(message: ChatbotMessage) {
    this.messages.push(message);
  }
}


/**
 * service gérant les conversations
 * passera probablement par un gestionnaire d'état
 */
@Injectable({
  providedIn: 'root'
})
export class ConversationsService {

  public chatbotConversations: Array<ChatbotConversation> = []


  constructor() {
    this.setDemoMessages()

  }

  setDemoMessages() {
    let demoMessages = [
      new ChatbotMessage({
        kind: 'user',
        user: {},
        content: "blah blah",
        name: "Demo Message"
      })
    ]
    const newConversation = new ChatbotConversation();
    newConversation.addMessage(new ChatbotMessage({
      kind: 'user',
      user: {},
      content: "blah blah",
      name: "Demo Message"
    }))

    this.chatbotConversations = [
      newConversation
    ]
  }

  addMessageToConversation(message: ChatbotMessage, conversation: ChatbotConversation) {
    conversation.addMessage(message)
  }

  addDemoMessage() {
    this.addMessageToConversation(new ChatbotMessage({
        kind: 'user',
        user: {},
        content: "blah blah",
        name: "Demo Message"
      }),
      this.chatbotConversations[0]
    )
  }
}
