import { Injectable, signal, WritableSignal } from '@angular/core';
import { SearchRepository } from '@app/domain/search/search.repository';
import { SearchFilters } from '@app/domain/search/search-filters';

@Injectable({
  providedIn: 'root',
})
export class SearchService implements SearchRepository {
  private filters = signal<SearchFilters>({
    search: '',
    verified: false,
    secteur: null,
    profession: null,
    sort: 'recent',
    page: 1,
    perPage: 25,
  });

  setFilters(filters: SearchFilters) {
    const filtersToSet = { ...this.filters(), ...filters };
    this.filters.set(filtersToSet);
  }

  filterByProfession(profession: string | null) {
    const filter = { ...this.filters(), profession };
    this.filters.set(filter);
  }

  filterByProfileVerified() {
    const filters = { ...this.filters(), verified: true };
    this.filters.set(filters);
  }

  filterBySecteur(secteur: string | null) {
    const filters = { ...this.filters(), secteur };
    this.filters.set(filters);
  }

  getFilters(): WritableSignal<SearchFilters> {
    return this.filters;
  }

  reset() {
    const filters = {
      ...this.filters(),
      verified: false,
      sort: 'recent',
      search: '',
      profession: null,
      secteur: null,
    };
    this.filters.set(filters);
  }

  search(query: string) {
    const filters = { ...this.filters(), search: query };
    this.filters.set(filters);
  }

  sortBy(value: string) {
    const filters = { ...this.filters(), sort: value };
    this.filters.set(filters);
  }
}
