import { Component, OnInit } from '@angular/core';
import * as Parse from 'parse';

@Component({
  selector: 'app-memo-history',
  templateUrl: './memo-history.page.html',
  styleUrls: ['./memo-history.page.scss'],
})
export class MemoHistoryPage implements OnInit {
  memos: any[] = [];

  constructor() {}

  ngOnInit() {
    this.loadMemos();
  }

  async loadMemos() {
    const currentUser = Parse.User.current();
    if (!currentUser) {
      console.error('No current user found');
      return;
    }

    const MemoHistory = Parse.Object.extend('memo_history');
    const query = new Parse.Query(MemoHistory);
    query.equalTo('username', currentUser.getUsername()); // Ensure only the current user's memos are fetched
    query.descending('memoId'); // Sort by memoId descending to get the latest memos first

    try {
      const results = await query.find();
      this.memos = results.map(memo => ({
        content: memo.get('content'),
        timestamp: memo.get('time'),
      }));
    } catch (error) {
      console.error('Error while fetching memo history:', error);
    }
  }
}
