import { Component, OnInit, inject, signal } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Router } from '@angular/router';
import { ArticleService } from '../../../core/services/article';
import { AuthService } from '../../../core/services/auth';
import { Article, CurrentUser } from '../../../shared/models/article';
import { environment } from '../../../../environments/environment';

@Component({
  selector: 'app-article-list',
  imports: [CommonModule],
  templateUrl: './article-list.html',
  styleUrl: './article-list.css'
})
export class ArticleList implements OnInit {
  private articleService = inject(ArticleService);
  private authService = inject(AuthService);
  private router = inject(Router);

  // SCAFFOLD pre-built Lab12: backendUrl pt thumbnail-uri imagini
  readonly backendUrl = environment.apiUrl;

  articles = signal<Article[]>([]);
  loading = signal(true);
  error = signal<string | null>(null);
  currentUser = signal<CurrentUser | null>(null);

  ngOnInit(): void {
    this.authService.currentUser$.subscribe(u => this.currentUser.set(u));
    this.loadArticles();
  }

  private loadArticles(): void {
    this.loading.set(true);
    this.articleService.getAll().subscribe({
      next: articles => {
        this.articles.set(articles);
        this.loading.set(false);
      },
      error: () => {
        this.error.set('Nu s-au putut incarca articolele');
        this.loading.set(false);
      }
    });
  }

  isAuthenticated(): boolean {
    return this.authService.isAuthenticated();
  }

  canModify(article: Article): boolean {
    const user = this.currentUser();
    if (!user) return false;
    return user.roles.includes('Admin') || article.authorId === user.id;
  }

  viewArticle(id: number): void {
    this.router.navigate(['/articles', id]);
  }

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

  editArticle(id: number, event: Event): void {
    event.stopPropagation();
    this.router.navigate(['/articles', id, 'edit']);
  }

  deleteArticle(id: number, event: Event): void {
    event.stopPropagation();
    if (!confirm('Sigur stergeti articolul?')) return;

    this.articleService.delete(id).subscribe({
      next: () => this.articles.update(arr => arr.filter(a => a.id !== id)),
      error: () => this.error.set('Eroare la stergere')
    });
  }
}
