import { Component, computed, inject, OnInit, signal } from '@angular/core';
import { ActivatedRoute, RouterOutlet } from '@angular/router';
import { User } from '@app/shared/models/user';
import { Location, UpperCasePipe } from '@angular/common';
import { UntilDestroy } from '@ngneat/until-destroy';
import { environment } from '@env/environment';
import { ChipsComponent } from '@app/shared/components/chips/chips.component';
import { ReseauxComponent } from '@app/shared/components/reseaux/reseaux.component';
import { UpdateUserComponent } from '@app/shared/features/update-user/update-user.component';
import { MyProfileProjectListComponent } from '@app/shared/components/my-profile-project-list/my-profile-project-list.component';
import { MyProfileUpdateFormComponent } from '@app/shared/components/my-profile-update-form/my-profile-update-form.component';
import { PdfViewerComponent } from '@app/shared/features/pdf-viewer/pdf-viewer.component';
import { ProfileFacade } from '@app/ui/profiles/profile.facade';

@Component({
  selector: 'app-my-profile',
  standalone: true,
  imports: [
    ChipsComponent,
    ReseauxComponent,
    UpdateUserComponent,
    MyProfileProjectListComponent,
    RouterOutlet,
    MyProfileUpdateFormComponent,
    PdfViewerComponent,
    UpperCasePipe,
  ],
  templateUrl: './my-profile.component.html',
  styleUrl: './my-profile.component.scss',
})
@UntilDestroy()
export class MyProfileComponent implements OnInit {
  protected readonly environment = environment;
  protected menu = signal<string>('home');

  protected myProfileQrCode = `${environment.production}`;

  protected location = inject(Location);
  protected readonly route = inject(ActivatedRoute);

  protected extraData: { user: User } = this.route.snapshot.data['user'];

  protected user = computed(() => {
    if (this.extraData != undefined) return this.extraData.user;
    return {} as User;
  });

  private readonly profileFacade = new ProfileFacade();
  protected profile = this.profileFacade.profile;
  protected readonly loading = this.profileFacade.loading;
  protected readonly error = this.profileFacade.error;

  ngOnInit(): void {
    this.myProfileQrCode = `${this.myProfileQrCode}/profiles/${this.user().id}`;
    this.profileFacade.loadOne(this.user().id);
  }
}
