import { Component, OnInit } from '@angular/core';
import {IonHeader, IonToolbar, IonTitle,IonInput, IonContent,IonButton, IonIcon } from '@ionic/angular/standalone';
import { addIcons } from 'ionicons';
import { chevronBack,heart } from 'ionicons/icons';
import { Router } from '@angular/router';
import { FmodeChatCompletion } from 'fmode-ng';

@Component({
  selector: 'app-page-companion',
  templateUrl: './page-companion.component.html',
  styleUrls: ['./page-companion.component.scss'],
  standalone: true,
  imports:[IonHeader, IonToolbar, IonTitle, IonContent,IonButton,IonInput, IonIcon]
})
export class PageCompanionComponent  implements OnInit {

  userInput: string = ''; // 用户输入内容
  messages: { role: string, content: string }[] = []; // 消息列表

  constructor() {}

  ngOnInit() {}

  // 用户输入变化
  onInputChange(ev: any) {
    this.userInput = ev.detail.value;
  }

  // 发送消息
  sendMessage() {
    if (!this.userInput.trim()) return; // 确保输入不为空

    // 添加用户消息到消息列表
    this.messages.push({ role: 'user', content: this.userInput });

    // 调用 AI 接口
    const promptTemplate = `
      作为一名专业的心理咨询师，请您根据用户的描述提供情感疏导和建议。
      以下是用户的口述：${this.userInput}
    `;

    const completion = new FmodeChatCompletion([
      { role: 'system', content: '' },
      { role: 'user', content: promptTemplate }
    ]);

    completion.sendCompletion().subscribe((message: any) => {
      // 添加 AI 消息到消息列表
      this.messages.push({ role: 'ai', content: message.content });
      // 清空用户输入框
      this.userInput = '';
    });
  }

  constructor(private router: Router) { 
    addIcons({chevronBack,heart});
  }

  goTab1(){
    this.router.navigate(['tabs/tab1']);
   }

  ngOnInit() {}

}
