import { Component, OnInit } from '@angular/core';
import { Note } from '../../models/note.model';
import { NoteService } from '../../services/note.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-note-list',
  templateUrl: './note-list.component.html',
  styleUrls: ['./note-list.component.scss']
})
export class NoteListComponent implements OnInit {
  notes: Note[] = [];
  selectedNote: Note | null = null;

  constructor(private noteService: NoteService, private router: Router) { }

  ngOnInit(): void {
    this.loadNotes();
  }

  loadNotes(): void {
    this.noteService.getNotes().subscribe(
      (notes) => {
        this.notes = notes;
      },
      (error) => {
        console.error('Error loading notes:', error);
      }
    );
  }

  viewNote(noteId: string): void {
    this.router.navigate(['/notes', noteId]);
  }

  editNote(noteId: string): void {
    this.router.navigate(['/notes', noteId, 'edit']);
  }

  deleteNote(noteId: string): void {
    if (confirm('Are you sure you want to delete this note?')) {
      this.noteService.deleteNote(noteId).subscribe(
        () => {
          this.loadNotes();
        },
        (error) => {
          console.error('Error deleting note:', error);
        }
      );
    }
  }

  createNewNote(): void {
    this.router.navigate(['/notes/new']);
  }
}
