import {Component} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {mergeMap, Subscription} from 'rxjs';
import {Post} from '../../models/post';
import {PostService} from '../../services/post.service';
import {HeaderComponent} from '../../components/header/header.component';
import {DatePipe} from '@angular/common';
import {CommentService} from '../../services/comment.service';

@Component({
  selector: 'app-post',
  standalone: true,
  imports: [
    HeaderComponent,
    DatePipe
  ],
  templateUrl: './post.component.html',
  styleUrl: './post.component.css'
})
export class PostComponent {
  subs: Subscription[] = [];
  concernedPost: Post | undefined;

  constructor(private route: ActivatedRoute,
              private postService: PostService,
              private commentService: CommentService) {
    this.route.paramMap.subscribe(params => {
      const postId = params.get('postId');
      if (postId) {
        this.subs.push(
          this.postService.getPost(BigInt(postId)).pipe(
            mergeMap(post => this.commentService.list(post.id))
          ).subscribe(post => {
            this.concernedPost = post;
          })
        );
      }
    });

  }

}
