import { Component, inject, signal } from '@angular/core';
import { SearchComponent } from '@app/shared/features/search/search.component';
import { VerticalProfileListComponent } from '@app/shared/components/vertical-profile-list/vertical-profile-list.component';
import { UntilDestroy } from '@ngneat/until-destroy';
import { ProfileFacade } from '@app/adapters/profiles/profile.facade';
import { LoadingComponent } from '@app/shared/components/loading/loading.component';
import { Router } from '@angular/router';
import { SearchFilters } from '@app/domain/search/search-filters';
import { FilterComponent } from '@app/shared/features/filter/filter.component';
import { PaginationComponent } from '@app/shared/features/pagination/pagination.component';
import { ProfileListMapComponent } from '@app/shared/features/profile-list-map/profile-list-map.component';

type ViewMode = 'list' | 'map';

@Component({
  selector: 'app-profile-list',
  standalone: true,
  imports: [
    SearchComponent,
    VerticalProfileListComponent,
    LoadingComponent,
    FilterComponent,
    PaginationComponent,
    ProfileListMapComponent,
  ],
  templateUrl: './profile-list.component.html',
  styleUrl: './profile-list.component.scss',
})
@UntilDestroy()
export class ProfileListComponent {
  private readonly facade = inject(ProfileFacade);
  private readonly router = inject(Router);

  protected readonly searchFilters = this.facade.searchFilters;
  protected readonly profilePaginated = this.facade.profilePaginated;
  protected readonly loading = this.facade.loading;
  protected readonly error = this.facade.error;

  protected readonly viewMode = signal<ViewMode>('list');

  showNewQuery(filters: SearchFilters) {
    this.facade.load(filters);
    this.router.navigate(['/profiles'], { queryParams: { search: filters.search } });
  }

  onFilterChange(filters: SearchFilters) {
    this.facade.load(filters);
  }

  onPageChange(filters: SearchFilters) {
    this.facade.load(filters);
  }

  switchToListView(): void {
    this.viewMode.set('list');
  }

  switchToMapView(): void {
    this.viewMode.set('map');
  }
}
